汇总以仅保留总计并删除小计

Posted

技术标签:

【中文标题】汇总以仅保留总计并删除小计【英文标题】:ROLL UP to keep only Grand Total and remove sub totals 【发布时间】:2021-05-13 05:02:27 【问题描述】:

我对以下查询输出有 2 个问题:


    COALESCE(Division,'Grand Total') Divi,
    COALESCE(Section,'DivTotal') Sec,
    COALESCE(m.Department,'SecTotal') Dept,
    COALESCE(Assortment,'DeptTotal') Assort,
    COALESCE(Promo,'') Prom,
    COALESCE(Range_Details,'') Range_,
    SUM(`WH STK`) WH_STK,
    SUM(`AL STK QTY`) AL_STK,
    SUM(`AL SAL QTY`) AL_SAL,
    SUM(`AL LYa`) LY_a,
    SUM(`AL LYb`) LY_b,
    SUM(`AL MIN`) AL_MIN,
    b.LYa_tot,
    b.LYb_tot,
    COALESCE(ROUND(((SELECT b.LYb_tot)/(SELECT b.LYa_tot))* COALESCE(SUM(`AL SAL QTY`),0),0),0) 'upcoming',
    COALESCE((ROUND(((SELECT b.LYb_tot)/(SELECT b.LYa_tot))*SUM(`AL SAL QTY`),0) +  SUM(`AL MIN`)) - SUM(`AL STK QTY`),0) 'Dispatch'

FROM
(.....I have removed rest of the query part to make it short....) m
GROUP BY
    m.Division,
    m.Section,
    m.Department,
    m.Assortment,
    m.Promo,
    m.Range_Details WITH ROLLUP HAVING
(Division IS NOT NULL AND Section IS NOT NULL AND Department IS NULL AND Assortment IS NULL AND Promo IS NULL AND Range_Details IS NULL )
OR (Division IS NOT NULL AND Section IS NOT NULL and Section NOT IN  ('OTHERS','W_ACCESSORIES','W_BOTTOMWEAR','W_INNERWEAR','W_UPPERWEAR') AND Department IS NOT NULL AND Assortment IS NULL AND Promo IS NULL AND Range_Details IS NULL )
OR (Division IS NOT NULL AND Section NOT IN  ('OTHERS','W_ACCESSORIES','W_BOTTOMWEAR','W_INNERWEAR','W_UPPERWEAR') AND Department IS NOT NULL and Department NOT IN ('GLOVE','RAINCOAT','SWIM SUIT','CORD TROUSERS','COTTON TROUSERS','FORMAL TROUSERS') AND Assortment IS NOT NULL AND Promo IS not NULL AND Range_Details IS not NULL )
OR (Division IS NULL AND Section IS NULL AND Department IS NULL AND Assortment IS NULL AND Promo IS NULL AND Range_Details IS NULL )

enter image description here 1.) 如何在最后一行只保留“Grand Total”并删除“DivTotal”、“SecTotal”、“DeptTotal”。

2.) 有没有更好的选择来使用“WITH ROLLUP”,这样我就可以只选择需要小计的列,而不是 GROUP BY 子句中所有列的小计?

注意:我已经在使用多个 UNION,所以如果可能,请给出没有联合的解决方案。 无法嵌入图片,给您带来的不便深表歉意。

【问题讨论】:

是的,ROLLUP 可能会在这里为您工作。请在您的问题中添加示例数据以使问题更清晰(仅查询对我们没有太大帮助)。 只需将AND Sec != 'DivTotal' AND Dept != 'SecTotal' AND Assort != 'DeptTotal') 添加到您的HAVING 子句中即可? @Nick 抱歉,但这会删除总计行。我需要将 Section、Department 和 Assortment 设为空白的总计行 啊抱歉,是的,你需要成功AND (Divi == 'Grand Total' OR Sec != 'DivTotal' AND Dept != 'SecTotal' AND Assort != 'DeptTotal')` 也不是那样工作的。我现在已经解决了,solutino 贴在下面。 【参考方案1】:

我认为 CTE 和摘要可能是一种更简单的方法:

with cte as (
      select m.Division, m.Section, m.Department, m.Assortment,  m.Promo, m.Range_Details,
             . . . 
      from . . .
      group by m.Division, m.Section, m.Department, m.Assortment,  m.Promo, m.Range_Details
     )
select *
from cte
union all
select 'Grand Total', null, null, null, null, null,
       sum(WH_STK), . . . 
from cte;

【讨论】:

我现在已经解决了,但稍后会尝试这种方法以找到它,如果它更有意义的话。谢谢戈登。【参考方案2】:

我以这种方式解决了这个问题,灵感来自 *** 本身的答案:

COALESCE(Division,'Grand Total') Divi,
(CASE WHEN Division IS NOT NULL THEN COALESCE(Section,'DivTotal') ELSE '' END) Sec,
(CASE WHEN Section IS NOT NULL THEN COALESCE(m.Department,'SecTotal') ELSE '' END) Dept,
(CASE WHEN m.Department IS NOT NULL THEN COALESCE(Assortment,' Total') ELSE '' END) Assort,
COALESCE(Promo,'') Prom,
(CASE WHEN Division IS NULL  THEN COALESCE(Range_Details,'Grand Total') ELSE COALESCE(Range_Details,'') END) Range_

感谢大家抽出宝贵时间。

【讨论】:

以上是关于汇总以仅保留总计并删除小计的主要内容,如果未能解决你的问题,请参考以下文章

sql添加小计和总计

group by子句中使用rollup和cube操作符

从 R 中的数据透视表库呈现的数据透视表中删除小计和总计

在数据库中保留小计字段是否一个坏主意

使用数据透视表(熊猫)中的小计行时保留索引部分(不同的列)

计算所有项目总计到小计 Jquery