字段上的条件计数


69

如果我有一个这样的表:

jobId, jobName, Priority

因此,优先级可以是1到5之间的整数。

由于我需要此查询来生成报告图表,因此需要显示Jobid,Jobname和5个字段,分别称为Priority1,Priority2,Priority3,Priority4。优先5。

Priority1应该计算优先级字段值为1的行数。

Priority2应该计算优先级字段值为2的行数。

Priority3应该计算优先级字段值为3的行数。

等等

我将如何快速高效地做到这一点?


一个工作ID是否有多个记录?因此Jobid 1可能在表中出现10次,且优先级不同?另外,您正在使用哪个数据库?SQL服务器?MySQL的?
罗宾·

Answers:


124

我想你可能会

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,如果要删除它们并删除组,


80

使用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

1
这将起作用,但是会产生:警告:通过聚合或其他SET操作消除了空值。
tekumara

5

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

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  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

您可以针对自己加入表格:

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

-2

我需要显示Jobid,Jobname和5个称为Priority1,Priority2,Priority3,Priority4的字段。优先5。

您的查询设计有问题。您还将在每行中显示一个特定的作业,因此您将遇到以下情况:该行的四个优先级列的值为“ 0”,一个优先级列的值为“ 1”(该作业的优先级)否则最终您将对每一行的所有优先级重复计数。

您真的想在这里显示什么?


-5

尝试这个:

SELECT Count(Student_ID) as 'StudentCount' 
FROM CourseSemOne
where Student_ID=3 
Having Count(Student_ID) < 6 and Count(Student_ID) > 0;

2
由于这是一个完全错误的答案,请帮我们一个忙,然后删除它
Guyarad
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.