我有以下问题需要解决:
以下是我的询问:
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;
以下是我的数据:
答案 0 :(得分:2)
如果通过"简化"你的意思是"使计算更简洁"然后是。以下内容适用:
COUNT(CASE WHEN source IN ('INTERNET', 'SALES', 'REP', 'COM') THEN 1 END)
只需使用nvl
将NULL转换为值:NVL(status, 'TOTAL') AS row_labels
ROLLUP( (status) )
与ROLLUP( status )