[英]COUNT with DISTINCT
我在MyTable中存储了客户的所有送货信息 - CustomerId,ShippingID,ItemID,ShipDate。 货件/ shippingID可以包含一个或多个Items / ItemID。 运输ID只能有一个发货日期。 这是表格的一个示例 -
CustomerID, ShippingID, ItemID, ShipDate
C1, A1, I200, today
C1, A1, I88, today
C1, A2, I7, tomorrow
C1, B1, I955, yesterday
C2, B2.....et cetra
对于customerID,我想显示不同的shippingID,shippingID,ShipDate中的项目数。
预期产量 -
C1, A1, 2, today
C1, A2, 1, tomorrow
C1, B1, 1, yesterday
...etc
我试过了 -
select distinct shippingid,
count(*) over() itemid,
orderdate
from mytable
where customerID = 'C1'
输出 -
C1, A1, 4, today
C1, A2, 4, tomorrow
问题是它计算C1的所有项目。 我想只计算客户的shippingID中的项目。 我该怎么做呢 ?
编辑 - 现在,我不需要它为所有客户。 只为一个。 因此没有必要分组。
谢谢。
SELECT shippingid,
COUNT(*) Items,
ShipDate
FROM mytable
WHERE customerID = 'C1'
GROUP BY shippingid,
ShipDate
为什么不使用Group By
? 这是最简单的方法...... SqlFiddle
SELECT
CustomerID,
ShippingID,
count(1)
FROM mytable
GROUP BY CustomerID, ShippingID
也许这个:
select customerID, shippingid, itemid, count(1)
from mytable
group by customerId, shippingid, itemid
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.