SQL服务器中的汇总和多维数据集的小计和总计

时间:2012-06-13 12:41:14

标签: sql sql-server subtotal

我可以在月视图中获得我们商品的销售额 喜欢:         项目 - 项目组代码 - 一月销售 - 二月销售 - .... - 十二月销售

使用此查询:

    SELECT       
    KART.CODE AS CODE,
    KART.DEFINITION_ AS NAME, 
    KART.SPECODE AS 'GROUP CODE',
    SUM(TRNS.TOTAL) AS [YILLIK TOPLAM],  
    SUM(CASE WHEN MONTH(TRNS.DATE_) = 1 THEN TRNS.TOTAL END) AS [OCAK],  
    SUM(CASE WHEN MONTH(TRNS.DATE_) = 2 THEN TRNS.TOTAL END) AS [ŞUBAT],  
    SUM(CASE WHEN MONTH(TRNS.DATE_) = 3 THEN TRNS.TOTAL END) AS [MART],  
    SUM(CASE WHEN MONTH(TRNS.DATE_) = 4 THEN TRNS.TOTAL END) AS [NİSAN],  
    SUM(CASE WHEN MONTH(TRNS.DATE_) = 5 THEN TRNS.TOTAL END) AS [MAYIS],  
    SUM(CASE WHEN MONTH(TRNS.DATE_) = 6 THEN TRNS.TOTAL END) AS [HAZİRAN],  
    SUM(CASE WHEN MONTH(TRNS.DATE_) = 7 THEN TRNS.TOTAL END) AS [TEMMUZ],  
    SUM(CASE WHEN MONTH(TRNS.DATE_) = 8 THEN TRNS.TOTAL END) AS [AĞUSTOS],  
    SUM(CASE WHEN MONTH(TRNS.DATE_) = 9 THEN TRNS.TOTAL END) AS [EYLÜL],  
    SUM(CASE WHEN MONTH(TRNS.DATE_) = 10 THEN TRNS.TOTAL END) AS [EKİM],  
    SUM(CASE WHEN MONTH(TRNS.DATE_) = 11 THEN TRNS.TOTAL END) AS [KASIM],  
    SUM(CASE WHEN MONTH(TRNS.DATE_) = 12 THEN TRNS.TOTAL END) AS [ARALIK]  

    FROM LG_001_07_STLINE TRANS LEFT OUTER JOIN LG_001_SRVCARD ITEM 
    on TRANS.STOCKREF=ITEM.LOGICALREF                        
    GROUP BY ITEM.SPECODE, ITEM.CODE, ITEM.DEFINITION_  
    ORDER BY ITEM.CODE

但是,当我插入条款

    GROUP BY ROLLUP (ITEM.SPECODE, ITEM.CODE, ITEM.DEFINITION_) 

获取小计,每个项目通常是小计的。这对我来说毫无意义。

当我只插入

    GROUP BY ROLLUP (ITEM.SPECODE)

我收到错误消息:

  

.....在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。

查看相同组代码的销售小计的解决方案是什么?

0 个答案:

没有答案