[英]Oracle SQL - simplifying the totals column
I have the below issues that I would like to address: 我要解决以下问题:
Below is my query: 以下是我的查询:
SELECT
status AS "ROW LABELS",
COUNT(case when source = 'INTERNET' THEN 1 end) AS "INTERNET",
COUNT(case when source = 'SALES' THEN 1 end) AS "SALES",
COUNT(case when source = 'REP' THEN 1 end) AS "REP",
COUNT(case when source = 'COM' THEN 1 end) AS "COM",
(COUNT(case when source = 'INTERNET' THEN 1 end) +
COUNT(case when source = 'SALES' THEN 1 end) +
COUNT(case when source = 'REP' THEN 1 end) +
COUNT(case when source = 'COM' THEN 1 end)
) AS Total
FROM
SOMETABLE
GROUP BY ROLLUP((status))
order by 1;
Below is my data: 以下是我的数据:
If by "simplify" you mean "make the calculation more succinct", then yes. 如果用“简化”来表示“使计算更简洁”,则可以。 The following will work:
以下将起作用:
COUNT(CASE WHEN source IN ('INTERNET', 'SALES', 'REP', 'COM') THEN 1 END)
Simply use nvl
to convert the NULL to a value: NVL(status, 'TOTAL') AS row_labels
只需使用
nvl
将NULL转换为值: NVL(status, 'TOTAL') AS row_labels
ROLLUP( (status) )
is the same as ROLLUP( status )
ROLLUP( (status) )
与ROLLUP( status )
相同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.