我有 2 个表,Order 和 Store。这是我需要的表中的一些字段。
订单表
OrderId StoreId SystemOrderStatus
1 1 Received
2 1 Sent
3 2 Complete
4 2 Received
我怎样才能得到这个输出:
StoreId ReceivedStatusCount SentStatusCount CompleteStatusCount
1 1 1 0
2 1 0 1
谢谢。
最佳答案
只需使用case when
,如下所示:
select
StoreId,
sum(case when SystemOrderStatus='Received' then 1 else 0 end) as ReceivedStatusCount,
sum(case when SystemOrderStatus='Sent' then 1 else 0 end) as SentStatusCount,
sum(case when SystemOrderStatus='Complete' then 1 else 0 end) as CompleteStatusCount
from
"Order"
group by
StoreId
order by
StoreId
关于c# - Group By 统计相同状态值的总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56453623/