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 6546263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:41:13+00:00 2026-05-25T11:41:13+00:00

Please note that I have seen a similar query here , but think my

  • 0

Please note that I have seen a similar query here, but think my query is different enough to merit a separate question.

Suppose that there is a database with the following tables:

  1. customer_table with customer_ID (key field), customer_name
  2. orders_table with order_ID (key field), customer_ID, product_ID

Now suppose I would like to find the names of all the customers who have ordered more than 10 different types of product, and the number of types of products they ordered. Multiple orders of the same product does not count.

I think the query below should work, but have the following questions:

  1. Is the use of count(distinct xxx) generally allowed with a “group by” statement?
  2. Is the method I use the standard way? Does anybody have any better ideas (e.g. without involving temporary tables)?

Below is my query

select T1.customer_name, T1.customer_ID, T2.number_of_products_ordered
from customer_table T1
inner join 
(
    select cust.customer_ID as customer_identity, count(distinct ord.product_ID) as number_of_products_ordered
    from customer_table cust
    inner join order_table ord on cust.customer_ID=ord.customer_ID
    group by ord.customer_ID, ord.product_ID
    having count(distinct ord.product_ID) > 10
) T2
on T1.customer_ID=T2.customer_identity
order by T2.number_of_products_ordered, T1.customer_name
  • 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-05-25T11:41:13+00:00Added an answer on May 25, 2026 at 11:41 am

    Isn’t that what you are looking for? Seems to be a little bit simpler. Tested it on SQL Server – works fine.

    SELECT customer_name, COUNT(DISTINCT product_ID) as products_count FROM customer_table
    INNER JOIN orders_table ON customer_table.customer_ID = orders_table.customer_ID
    GROUP BY customer_table.customer_ID, customer_name
    HAVING COUNT(DISTINCT product_ID) > 10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Please note that I have seen a similar question on StackOverflow recently, however I
[Please note that this is a different question from the already answered How to
Please note that this question is from 2008 and now is of only historic
I have seen this question asked in a couple of different ways on SO
(Note: I have seen several questions regarding .NET logging frameworks, but haven't seen any
Please note that this is not homework and i did search before starting this
Please note that I did ask this on Super User and received absolutely no
I mean things like: FK1 -> 1FK2 -> 2PK Please, note that 1FK2 is
Please note: This is a question about the Eclipse plugin Subversive , and not
If I want to use a decimal literal in code, I have seen that

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.