2009-08-17 54 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