简体   繁体   English

Oracle SQL-简化总计列

[英]Oracle SQL - simplifying the totals column

I have the below issues that I would like to address: 我要解决以下问题:

  1. Is there a way to simplify the Total column? 有没有一种方法可以简化“总计”列?
  2. The bottom row reads as null, I would like that to be "Total" as well. 最下面一行为空,我也希望它也为“总计”。
  3. Is it better to ROLLUP the way I have it, ROLLUP((status))? 以我的方式进行ROLLUP更好吗,ROLLUP((status))? Or this is exactly same as ROLLUP(status)? 或者这与ROLLUP(status)完全相同?

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: 以下是我的数据:

在此处输入图片说明

  1. 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) 
  2. Simply use nvl to convert the NULL to a value: NVL(status, 'TOTAL') AS row_labels 只需使用nvl将NULL转换为值: NVL(status, 'TOTAL') AS row_labels

  3. ROLLUP( (status) ) is the same as ROLLUP( status ) ROLLUP( (status) )ROLLUP( status )相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM