检索购买了超过13种不同产品而从未购买过同一产品的客户

时间:2018-06-29 00:45:18

标签: mysql sql-server adventureworks

我尝试过了。但是我觉得这给订购相同产品的人

SELECT DISTINCT Count(od.orderqty) OrderQty, 
                c.customerid, 
                od.productid 
FROM   sales.customer c 
       INNER JOIN sales.salesorderheader oh 
               ON c.customerid = oh.customerid 
       INNER JOIN sales.salesorderdetail od 
               ON oh.salesorderid = od.salesorderid 
GROUP  BY od.productid, 
          c.customerid 
HAVING Count(od.productid) > 10 
ORDER  BY c.customerid 

1 个答案:

答案 0 :(得分:0)

不确定使用的是哪种SQL,请尝试以下操作:

select  t.CustomerID
from    (
select  c.CustomerID
        , count(distinct od.ProductID) as DistinctCount
        , count(od.ProductID) as Count
from    Sales.Customer c
join    Sales.SalesOrderHeader oh
        on c.customerid = oh.customerid
join    Sales.SalesOrderDetail od
        on oh.SalesOrderID = od.SalesOrderID
group 
by      c.CustomerID 
) as t
where   t.DistinctCount = t.Count
and     t.DistinctCount > 13
order 
by      t.CustomerID