2009-08-17 194 views
47

如果我有像這樣的表:條件計數

jobId, jobName, Priority 

。由此可以優先於5.

1之間的整數。由於我需要此查詢生成的報告圖表,我需要顯示jobid,jobname和稱爲Priority1,Priority2,Priority3,Priority4的5個字段。 Priority5。

應的優先級爲計數,其中優先級字段具有1

的值的行的量

優先級2應計數,其中優先級字段具有2

的值的行的量

Priority3應該數的量行,其中優先級字段的值爲3

等價值

我會怎麼做,在一個快速,高性能的方式?

非常感謝, 卡瓦

+0

你有多個記錄的單個工作ID?所以jobid 1可能在表格中有10次,全部都有不同的優先級?還有,你使用的是哪個數據庫? SQL服務器? MySQL的? – 2009-08-17 13:54:14

回答

87

我想你可能是後

select 
    jobID, JobName, 
    sum(case when Priority = 1 then 1 else 0 end) as priority1, 
    sum(case when Priority = 2 then 1 else 0 end) as priority2, 
    sum(case when Priority = 3 then 1 else 0 end) as priority3, 
    sum(case when Priority = 4 then 1 else 0 end) as priority4, 
    sum(case when Priority = 5 then 1 else 0 end) as priority5 
from 
    Jobs 
group by 
    jobID, JobName 

但我不確定是否你需要在你的結果中找到jobID和JobName,如果這樣刪除它們並刪除組,

1
SELECT Priority, COALESCE(cnt, 0) 
FROM (
     SELECT 1 AS Priority 
     UNION ALL 
     SELECT 2 AS Priority 
     UNION ALL 
     SELECT 3 AS Priority 
     UNION ALL 
     SELECT 4 AS Priority 
     UNION ALL 
     SELECT 5 AS Priority 
     ) p 
LEFT JOIN 
     (
     SELECT Priority, COUNT(*) AS cnt 
     FROM jobs 
     GROUP BY 
       Priority 
     ) j 
ON  j.Priority = p.Priority 
-1

我需要顯示作業ID,作業名和第5場被稱爲優先級爲1,優先級2,Priority3,Priority4。 Priority5。

你的查詢設計出了問題。你也在每一行顯示一個特定的工作,所以你可能會遇到這樣一種情況,行中有四個優先級列'0'和一個優先級列'1'(該作業的優先級)否則你最終會重複每行所有優先級的計數。

你真的想在這裏展示什麼?

4

使用ANSI SQL-92 CASE語句,你可以做這樣的事情(派生表加時):

SELECT jobId, jobName, SUM(Priority1) 
AS Priority1, SUM(Priority2) AS 
Priority2, SUM(Priority3) AS 
Priority3, SUM(Priority4) AS 
Priority4, SUM(Priority5) AS 
Priority5 FROM (
    SELECT jobId, jobName, 
    CASE WHEN Priority = 1 THEN 1 ELSE 0 END AS Priority1, 
    CASE WHEN Priority = 2 THEN 1 ELSE 0 END AS Priority2, 
    CASE WHEN Priority = 3 THEN 1 ELSE 0 END AS Priority3, 
    CASE WHEN Priority = 4 THEN 1 ELSE 0 END AS Priority4, 
    CASE WHEN Priority = 5 THEN 1 ELSE 0 END AS Priority5 
    FROM TableName 


) 
1

你可以自己加入表格:

select 
    t.jobId, t.jobName, 
    count(p1.jobId) as Priority1, 
    count(p2.jobId) as Priority2, 
    count(p3.jobId) as Priority3, 
    count(p4.jobId) as Priority4, 
    count(p5.jobId) as Priority5 
from 
    theTable t 
    left join theTable p1 on p1.jobId = t.jobId and p1.jobName = t.jobName and p1.Priority = 1 
    left join theTable p2 on p2.jobId = t.jobId and p2.jobName = t.jobName and p2.Priority = 2 
    left join theTable p3 on p3.jobId = t.jobId and p3.jobName = t.jobName and p3.Priority = 3 
    left join theTable p4 on p4.jobId = t.jobId and p4.jobName = t.jobName and p4.Priority = 4 
    left join theTable p5 on p5.jobId = t.jobId and p5.jobName = t.jobName and p5.Priority = 5 
group by 
    t.jobId, t.jobName 

或者你可以使用情況之內:

select 
    jobId, jobName, 
    sum(case Priority when 1 then 1 else 0 end) as Priority1, 
    sum(case Priority when 2 then 1 else 0 end) as Priority2, 
    sum(case Priority when 3 then 1 else 0 end) as Priority3, 
    sum(case Priority when 4 then 1 else 0 end) as Priority4, 
    sum(case Priority when 5 then 1 else 0 end) as Priority5 
from 
    theTable 
group by 
    jobId, jobName 
59

使用COUNT代替SUM消除了一個ELSE語句的要求:

SELECT jobId, jobName, 
    COUNT(CASE WHEN Priority=1 THEN 1 END) AS Priority1, 
    COUNT(CASE WHEN Priority=2 THEN 1 END) AS Priority2, 
    COUNT(CASE WHEN Priority=3 THEN 1 END) AS Priority3, 
    COUNT(CASE WHEN Priority=4 THEN 1 END) AS Priority4, 
    COUNT(CASE WHEN Priority=5 THEN 1 END) AS Priority5 
FROM TableName 
GROUP BY jobId, jobName 
+0

這會工作,但會產生: 警告:空值由集合或其他SET操作消除。 – tukushan 2013-04-14 22:04:40

2

IIF不是一個標準的SQL構造,但如果它受到數據庫的支持,則可以實現更加優雅的語句,從而產生相同的結果:

SELECT JobId, JobName, 

COUNT(IIF (Priority=1, 1, NULL)) AS Priority1, 
COUNT(IIF (Priority=2, 1, NULL)) AS Priority2, 
COUNT(IIF (Priority=3, 1, NULL)) AS Priority3, 
COUNT(IIF (Priority=4, 1, NULL)) AS Priority4, 
COUNT(IIF (Priority=5, 1, NULL)) AS Priority5 

FROM TableName 
GROUP BY JobId, JobName 
-1

SELECT Count(Student_ID)as'StudentCount'FROM CourseSemOne where Student_ID = 3有計數(Student_ID)< 6和Count(Student_ID)> 0;

+1

由於這是一個完全錯誤的答案,請大家幫忙,並將其刪除 – guyarad 2016-09-06 05:28:51