oracle 基于条件的列数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33169707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Column count based on a condition
提问by Alex Man
I have created an oracle query like as shown below,the query is working fine but the problem is that I want one more column which is the count of name where category should be A and id should be 1
我创建了一个如下所示的 oracle 查询,查询工作正常,但问题是我还想要一列,它是名称的计数,其中类别应为 A,id 应为 1
SELECT name, velocity, COUNT(*) AS count, category FROM section GROUP BY name, velocity
Can anyone please tell me some solution for this
谁能告诉我一些解决方案
回答by Canburak Tümer
SELECT name, velocity, COUNT(*) AS count,
COUNT(CASE WHEN category = 'A' AND id = 1 THEN 1 END)
FROM section
GROUP BY name, velocity
This should work.
这应该有效。
If record does not meet the condition then it will return a NULL, and count skips NULL fields.
如果记录不满足条件,那么它将返回一个 NULL,并且 count 跳过 NULL 字段。
回答by Tatiana
Something like this:
像这样的东西:
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
回答by juergen d
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