c# - Group By 统计相同状态值的总数

标签 c# sql group-by count linq-to-sql

我有 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/

相关文章:

c# - 反射调用方法 - 参数异常

sql - 如何更改 Oracle 中 View 的列大小

c# - 如何从 tiff 图像中裁剪一部分

c# - 我应该使用 System.Guid.NewGuid() 还是使用 System 然后使用 Guid.NewGuid()?

c# - 如何使 ASP.NET Core Web API 操作异步执行?

Moodle 上类(class)注册的 SQL 查询

PHP/MYSQL : formatting user input data as MYSQL timestamp to search mySQL table

c# - LINQ Group By 并选择集合

sql - 我应该如何修改这个 SQL 语句?

python - Pandas 对每个唯一服务器的结果进行计数