SQL-子查询计数

SQL-子查询计数,sql,count,subquery,Sql,Count,Subquery,我已经启动了以下代码 SELECT distinct crq_risk ,count(crq_risk) as 'count' ,( select count(datediff(day,submit_date,crq_scheduled_for_approval_date)) from change_information where crq_risk = 'Risk Level 1' and submit_date

我已经启动了以下代码

SELECT distinct crq_risk 
    ,count(crq_risk) as 'count'
    ,(  select count(datediff(day,submit_date,crq_scheduled_for_approval_date)) 
        from change_information
        where crq_risk = 'Risk Level 1'
            and submit_date >= '2016-05-01 00:00:00.000'
            and crq_scheduled_for_approval_date IS NOT NULL
            and crq_change_timing = 'Non Standard - Planned'
            and datediff (day,submit_date,crq_scheduled_for_approval_date) < 10) as RL1
   ,(   select count(datediff(day,submit_date,crq_scheduled_for_approval_date)) 
        from change_information
        where crq_risk = 'Risk Level 2'
            and submit_date >= '2016-05-01 00:00:00.000'
            and crq_scheduled_for_approval_date IS NOT NULL
            and crq_change_timing = 'Non Standard - Planned'
            and datediff (day,submit_date,crq_scheduled_for_approval_date) < 10) as RL2
    ,(  select count(datediff(day,submit_date,crq_scheduled_for_approval_date)) 
        from change_information
        where crq_risk = 'Risk Level 3'
            and submit_date >= '2016-05-01 00:00:00.000'
            and crq_scheduled_for_approval_date IS NOT NULL
            and crq_change_timing = 'Non Standard - Planned'
            and datediff (day,submit_date,crq_scheduled_for_approval_date) < 5) as RL3
   ,(   select count(datediff(day,submit_date,crq_scheduled_for_approval_date)) 
        from change_information
        where crq_risk = 'Risk Level 4'
            and submit_date >= '2016-05-01 00:00:00.000'
            and crq_scheduled_for_approval_date IS NOT NULL
            and crq_change_timing = 'Non Standard - Planned'
            and datediff (day,submit_date,crq_scheduled_for_approval_date) < 3) as RL4
from change_information
where submit_date >= '2016-05-01 00:00:00.000'
    and crq_scheduled_for_approval_date IS NOT NULL
    and crq_change_timing = 'Non Standard - Planned'
group by crq_risk
您可以将
sum()
case语句一起使用

SELECT distinct crq_risk 
    ,count(crq_risk) as 'count'
    ,sum( case when ((crq_risk in ('Risk Level 1', 'Risk Level 2') and datediff (day,submit_date,crq_scheduled_for_approval_date) < 10)
                    or (crq_risk = 'Risk Level 3' and datediff (day,submit_date,crq_scheduled_for_approval_date) < 5)
                    or (crq_risk = 'Risk Level 4' and datediff (day,submit_date,crq_scheduled_for_approval_date) < 3))
            then 1 else 0 end 
        ) as edited_count
from change_information
where submit_date >= '2016-05-01 00:00:00.000'
    and crq_scheduled_for_approval_date IS NOT NULL
    and crq_change_timing = 'Non Standard - Planned'
group by crq_risk;
选择不同的crq\U风险
,将(crq_风险)计算为“计数”
,总和(当(crq风险处于(“风险等级1”、“风险等级2”)和日期差异(日期、提交日期、crq计划审批日期)<10时)
或(crq\U风险=‘风险等级3’和日期差异(日期、提交日期、crq\U计划批准日期)<5)
或(crq_风险=‘风险等级4’和日期差异(日期、提交日期、crq_计划批准日期)<3))
然后1或0结束
)已编辑的U计数
从更改信息
其中提交日期>='2016-05-01 00:00:00.000'
并且crq\U计划的批准日期不为空
crq_变更_时间='非标准-计划'
crq_风险集团;
您可以将
sum()
案例语句一起使用

SELECT distinct crq_risk 
    ,count(crq_risk) as 'count'
    ,sum( case when ((crq_risk in ('Risk Level 1', 'Risk Level 2') and datediff (day,submit_date,crq_scheduled_for_approval_date) < 10)
                    or (crq_risk = 'Risk Level 3' and datediff (day,submit_date,crq_scheduled_for_approval_date) < 5)
                    or (crq_risk = 'Risk Level 4' and datediff (day,submit_date,crq_scheduled_for_approval_date) < 3))
            then 1 else 0 end 
        ) as edited_count
from change_information
where submit_date >= '2016-05-01 00:00:00.000'
    and crq_scheduled_for_approval_date IS NOT NULL
    and crq_change_timing = 'Non Standard - Planned'
group by crq_risk;
选择不同的crq\U风险
,将(crq_风险)计算为“计数”
,总和(当(crq风险处于(“风险等级1”、“风险等级2”)和日期差异(日期、提交日期、crq计划审批日期)<10时)
或(crq\U风险=‘风险等级3’和日期差异(日期、提交日期、crq\U计划批准日期)<5)
或(crq_风险=‘风险等级4’和日期差异(日期、提交日期、crq_计划批准日期)<3))
然后1或0结束
)已编辑的U计数
从更改信息
其中提交日期>='2016-05-01 00:00:00.000'
并且crq\U计划的批准日期不为空
crq_变更_时间='非标准-计划'
crq_风险集团;

始终标记您使用的DBMS(SQL Server、Oracle、MySQL、PostgreSQL、SQLite、MS Access…),因为每种方言都会有所不同。始终标记您使用的DBMS(SQL Server、Oracle、MySQL、PostgreSQL、SQLite、MS Access…),因为每种方言都会有所不同。谢谢@Parfait。但这是根据
crq\u风险
分组的总和,因此
编辑的\u计数
对于
风险等级1
2
将有所不同。它是
案例陈述
而不是
计数
。测试和工作:)谢谢@Parfait。但这是根据
crq\u风险
分组的总和,因此
编辑的\u计数
对于
风险等级1
2
将有所不同。它是
案例陈述
而不是
计数
。测试和工作:)