基于条件的列数

时间:2015-10-16 11:45:27

标签: oracle count

我已经创建了一个如下所示的oracle查询,查询工作正常 但问题是我想要一个列,这是名称的计数,其中类别应为A,id应为1

    October 2015
Mo     5 12 19 26
Tu     6 13 20 27
We     7 14 21 28
Th  1  8 15 22 29
Fr  2  9 16 23 30
Sa  3 10 17 24 31
Su  4 11 18 25

enter image description here

任何人都可以告诉我一些解决方案吗

3 个答案:

答案 0 :(得分:21)

SELECT name, velocity, COUNT(*) AS count, 
COUNT(CASE WHEN category = 'A' AND id = 1 THEN 1 END)
FROM section 
GROUP BY name, velocity

这应该有用。

如果记录不符合条件,则它将返回NULL,并且count将跳过NULL字段。

答案 1 :(得分:2)

这样的事情:

SELECT name, velocity, COUNT(*) AS count, 
SUM(CASE WHEN category = 'A' AND id = 1 THEN 1 ELSE 0 END)
FROM section 
GROUP BY name, velocity

答案 2 :(得分:1)

SELECT name, velocity, COUNT(*) AS count, category, 
       (select count(distinct name) from section where category = 'A' and id = 1)
FROM section 
GROUP BY name, velocity