c# - 从两个 where 条件返回 SQL 值以在 C# 中显示

标签 c# sql sql-server

我有一个表,其中有一个 boolean 字段 扫描。

我想在 C# WinForm 中显示已完成和未决扫描的计数。

我在 SQL 中使用存储过程。

select count(1),sum(pages),count(distinct custId) from TableA where scanning=0 --Pending
select count(1),sum(pages),count(distinct custId) from TableA where scanning=1 --Done

我需要在 SQL 中创建两个存储过程,还是有办法从单个存储过程中获取结果。

最佳答案

SELECT
    PendingCnt = COUNT_BIG(CASE WHEN scanning = 0 THEN 1 END),
    PendingSum = ISNULL(SUM(CASE WHEN scanning = 0 THEN pages END), 0),
    PendingCustID = COUNT_BIG(DISTINCT CASE WHEN scanning = 0 THEN custId END),
    DoneCnt = COUNT_BIG(CASE WHEN scanning = 1 THEN 1 END),
    DoneSum = ISNULL(SUM(CASE WHEN scanning = 1 THEN pages END), 0),
    DoneCustID = COUNT_BIG(DISTINCT CASE WHEN scanning = 1 THEN custId END)
FROM TableA

关于c# - 从两个 where 条件返回 SQL 值以在 C# 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35768847/

相关文章:

c# - 异步/等待与线程

mysql - 如何从一列的结果列表中选择第二列作为引用?

mysql - 在 MYSQL 中使用内连接时获取总金额的问题

sql-server - SQL 中常量的最佳模式?

c# - 单例数据访问层

c# - LINQ,简化表达式 - take while take of sum of taken does not exceed given value

c# - “Microsoft.Rest.Azure.CloudException”,原因 : 'Microsoft. Rest.Azure.CloudException:此请求的授权已被拒绝

sql - Django /Postgres : Aggregate on RangeField

sql-server - 合并复制 - 触发器在发布者和订阅者上触发

sql-server - Sql where 子句在过滤器为空的情况下返回所有内容