Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8544187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:29:40+00:00 2026-06-11T12:29:40+00:00

I’m trying to get a query which would show distributors that haven’t sell anything

  • 0

I’m trying to get a query which would show distributors that haven’t sell anything in 90 days, but the problem I get is with NULL values. It seems PostgreSQL ignores null values, even when I queried to show it (or maybe I did it in wrong way).

Let say there are 1000 distributors, but with this query I only get 1 distributor, but there should be more distributors that didn’t sell anything, because if I write SQL query to show distributors that sold by any amount in the last 90 days, it shows about 500. So I wonder where are those other 499? If I understand correctly, those other 499, didn’t have any sales, so all records are null and are not showed in query.

Does anyone know how to make it show null values of one table where in relation other table is not null? (like partners table (res_partner) is not null, but sale_order table (sales) or object is null? (I also tried to filter like so.id IS NULL, but in such way I get empty query)

Code of my query:

(
SELECT
    min(f1.id) as id,
    f1.partner as partner,
    f1.sum1
       FROM
    (
       SELECT
          min(f2.id) as id,
          f2.partner as partner,
          sum(f2.null_sum) as sum1
       FROM
    (
    SELECT
          min(rp.id) as id,
          rp.search_name as partner,
     CASE
       WHEN
             sol.price_subtotal IS NULL
       THEN
             0
       ELSE
             sol.price_subtotal
     END as null_sum
    FROM
          sale_order as so,
          sale_order_line as sol,
          res_partner as rp
    WHERE
    sol.order_id=so.id and
    so.partner_id=rp.id
    and
    rp.distributor=TRUE
    and
    so.date_order <= now()::timestamp::date
    and
    so.date_order >= date_trunc('day', now() - '90 day'::interval)::timestamp::date
    and 
    rp.contract_date <= date_trunc('day', now() - '90 day'::interval)::timestamp::date
    GROUP BY
    partner,
    null_sum
   )as f2
   GROUP BY
   partner
  ) as f1
WHERE
sum1=0
GROUP BY
partner,
sum1
)as fld

EDIT: 2012-09-18 11 AM.

I think I understand why Postgresql behaves like this. It is because of the time interval. It checks if there is any not null value in that inverval. So it only found one record, because that record had sale order with zero (it was not converted from null to zero) and part which checked for null values was just skipped. If I delete time interval, then I would see all distributors that didn’t sell anything at all. But with time interval for some reason it stops checking null values and looks if there are only not null values.

So does anyone know how to make it check for null values too in given interval?.. (for the last 90 days to be exact)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:29:41+00:00Added an answer on June 11, 2026 at 12:29 pm

    Aggregates like sum() and and min() do ignore NULL values. This is required by the SQL standard and every DBMS I know behaves like that.

    If you want to treat a NULL value as e.g. a zero, then use something like this:

    sum(coalesce(f2.null_sum, 0)) as sum1
    

    But as far as I understand you question and your invalid query you actually want an outer join between res_partner and the sales tables.

    Something like this:

    SELECT min(rp.id) as id,
           rp.search_name as partner,
           sum(coalesce(sol.price_subtotal,0)) as price_subtotal
    FROM res_partner as rp 
      LEFT JOIN sale_order as so ON so.partner_id=rp.id and rp.distributor=TRUE
      LEFT JOIN sale_order_line as sol ON sol.order_id=so.id 
    WHERE so.date_order <= CURRENT_DATE
      and so.date_order >= date_trunc('day', now() - '90 day'::interval)::timestamp::date
      and rp.contract_date <= date_trunc('day', now() - '90 day'::interval)::timestamp::date
    GROUP BY rp.search_name
    

    I’m not 100% sure I understood your problem correctly, but it might give you a headstart.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.