Using SQL query to find details of customers who ordered > x types of products

后端 未结 2 1687
生来不讨喜
生来不讨喜 2021-01-03 10:10

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 fol

相关标签:
2条回答
  • 2021-01-03 10:51

    You could do it more simply:

    select
    
        c.id,
        c.cname,
        count(distinct o.pid) as `uniques`
    
    from o join c
    on c.id = o.cid
    
    group by c.id
    
    having `uniques` > 10
    
    0 讨论(0)
  • 2021-01-03 11:03

    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 讨论(0)
提交回复
热议问题