2016-05-19 2 views
0

Я начал следующий кодSQL - подзапрос подсчет

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 

Это приводит к

crq_risk  count RL1 RL2 RL3 RL4 
Risk Level 1 0  0.00 7  69 101 
Risk Level 2 8  0.00 7  69 101 
Risk Level 3 183  0.00 7  69 101 
Risk Level 4 247  0.00 7  69 101 

whereas I want the table to look like this 

crq_risk  count edited_count 
Risk Level 1 0  0 
Risk Level 2 8  7 
Risk Level 3 183 69 
Risk Level 4 247 101 
+0

Всегда помечать СУБД (SQL Server, Oracle, MySQL, PostgreSQL, SQLite, MS Access ...), которые вы используете, поскольку каждый диалект будет отличаться. – Parfait

ответ

2

Вы можете использовать sum() с case statement

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; 
+0

Спасибо @Parfait. Но это сумма - это группа 'crq_risk', поэтому' edit_count' будет отличаться для 'Risk Level 1' и' 2'. И это 'case statement' не' count'. Протестировано и сработало :) –