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

jjhzyzn0  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(296)

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

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
ubof19bj

ubof19bj1#

不确定您使用的是什么风格的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

相关问题