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

  • Home
  • SEARCH
  • 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 8465507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:04:09+00:00 2026-06-10T15:04:09+00:00

Sorry about the complicated title. I have two tables, customers and orders: customers –

  • 0

Sorry about the complicated title.

I have two tables, customers and orders:

customers – names may be duplicated, ids are unique:

name | cid
 a   |  1
 a   |  2
 b   |  3
 b   |  4
 c   |  5

orders – pid is unique, join on cid:

pid | cid | date
 1  |  1  | 01/01/2012
 2  |  1  | 01/01/2012
 3  |  2  | 01/01/2012
 4  |  3  | 01/01/2012
 5  |  3  | 01/01/2012
 6  |  3  | 01/01/2012

So I used this code to get a count:

select customers.name, orders.date, count(*) as count
from customers
left JOIN orders ON customers.cid = orders.cid
where date between '01/01/2012' and '02/02/2012'
group by name,date

which worked fine but didnt give me null rows when the cid of customers didnt match a cid in orders, e.g. name-c, id-5

select customers.name, orders.date, count(*) as count
from customers
left JOIN orders ON customers.cid = orders.cid
AND date between '01/01/2012' and '02/02/2012'
group by name,date

So I changed the where to apply to the join instead, which works fine, it gives me the null rows.
So in this example I would get:

name | date       | count
 a   | 01/01/2012 | 3
 b   | null       | 1
 b   | 01/01/2012 | 3
 c   | null       | 1

But because names have different cid’s it is giving me a null row even if the name itself does have rows in orders, which I don’t want.

So I’m looking for a way for the null rows to only be returned when any other cid’s that share the same name also do not have any rows in orders.

Thanks for any help.

—EDIT—

I have edited the counts for null rows, count never returns null but 1.

The result of

select * from (select customers.name, orders.date, count(*) as count
from customers
left JOIN orders ON customers.cid = orders.cid
AND date between '01/01/2012' and '02/02/2012'
group by name,date) as t1 group by name

is

name | date       | count
 a   | 01/01/2012 | 3
 b   | null       | 1
 c   | null       | 1
  • 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-10T15:04:10+00:00Added an answer on June 10, 2026 at 3:04 pm

    First, select your date grouped by (name, date), excluding NULLs, then join with a set of distinct names:

    SELECT names.name, grouped.date, grouped.count
    FROM ( SELECT DISTINCT name FROM customers ) as names
    LEFT JOIN (
      SELECT customers.name, orders.date, COUNT(*) as count
      FROM customers
      LEFT JOIN orders ON customers.cid = orders.cid
      WHERE date BETWEEN '01/01/2012' AND '02/02/2012'
      GROUP BY name,date
    ) grouped
    ON names.name = grouped.name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry about the extremely vague question title (any suggestions for improvements welcome) I have
Sorry about the wording for my question title. I have a basic HTML anchor
Sorry about the terrible title. I have a signed assembly 'Signed.dll' that is a
Sorry about the lame title. I have a project I'm working on and I
Sorry about the in-concise title. I have an Android app in which I draw
(sorry about the long title) I have a custom object Person, which in turn
Sorry about the vague title but I have a servlet with the following mapping
(Sorry about the title, couldn't think of how to explain it) So I have
Sorry about the vague question title, but I have these typedefs here: typedef std::list<AnimationKeyframe>
Sorry about the title, don't know how to phrase this. I have my own

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.