需要使用每个组的列值总和将列展平为每个行的单行

时间:2013-08-10 14:55:13

标签: sql-server tsql

我一直在尝试将查询中的数据展平为每个组的一行。我认为使用CASE语句可以实现这一点,但它仍然将结果放在单独的行中。我也在Group By子句中使用CASE语句。

    Actual Results:                     Desired Result:
    aonBom                      Fees    aonBom                      Fees
    AA201   D02440120105        9.00    AA201   D02440120105        15.00
    AA201   D02440120105        6.00    AA201   D14251210105        9.00
    AA201   D14251210105        9.00    AA201   D16007120115        9.00
    AA201   D16007120115        9.00    AA702   D02440120110        15.00
    AA702   D02440120110        9.00
    AA702   D02440120110        6.00

我当前的sql语句是:

SELECT
    CASE a.productid
        WHEN 6 THEN a.productid + space(2) + a.cusotmerid + p.progcode+ w.level 
        WHEN 7 THEN a.productid + space(1) + a.cusotmerid + p.progcode+ w.level
        ELSE a.productid + space(3) + a.cusotmerid + p.progcode + w.level
    END AS aonBom,  
    CASE        
        WHEN (s.sku IN('80-53-0000', '80-52-TTSB')
            or s.sku BETWEEN '90-00-0000' AND '90-00-9999')
        THEN sum(s.retail)
    END as Fees
FROM
    l
        INNER JOIN item ON l.product = i.prime
        INNER JOIN item AS s ON l.compon =s.prime
        INNER JOIN a ON i.prime = a.product
        INNER JOIN p ON a.program = p.prime
        INNER JOIN w ON a.award = w.prime
        INNER JOIN c ON p.customerid = c.prime
WHERE
    c.active = 'Active'
    AND p.status = 'Active'
GROUP BY    
    CASE a.productid
        WHEN 6 THEN a.productid + space(2) + a.cusotmerid + p.progcode+ w.level 
        WHEN 7 THEN a.productid + space(1) + a.cusotmerid + p.progcode+ w.level
        ELSE a.productid + space(3) + a.cusotmerid + p.progcode + w.level
    END,
    s.sku,
    c.active, 
    p.status
HAVING
    s.sku IN ('80-53-0000', '80-52-TTSB') OR
    (s.sku BETWEEN '90-00-0000' AND '90-00-9999')
ORDER BY
    aonBom

我已经准备好几篇网上的文章,使用CASE语句是压扁数据的方法,只是不确定我在这里做错了什么。

1 个答案:

答案 0 :(得分:0)

从我所看到的情况来看,我会将SUM从内部移到外部,移除GROUP BY中的最后3项并删除HAVING语句

这样的东西
SELECT
    CASE a.productid
        WHEN 6 THEN a.productid + space(2) + a.cusotmerid + p.progcode+ w.level 
        WHEN 7 THEN a.productid + space(1) + a.cusotmerid + p.progcode+ w.level
        ELSE a.productid + space(3) + a.cusotmerid + p.progcode + w.level
    END AS aonBom,  
    sum(CASE        
        WHEN (s.sku IN('80-53-0000', '80-52-TTSB') or s.sku BETWEEN '90-00-0000' AND '90-00-9999')
            THEN s.retail
        END) as Fees 
    FROM l
        INNER JOIN item ON l.product = i.prime
        INNER JOIN item AS s ON l.compon =s.prime
        INNER JOIN a ON i.prime = a.product
        INNER JOIN p ON a.program = p.prime
        INNER JOIN w ON a.award = w.prime
        INNER JOIN c ON p.customerid = c.prime WHERE
    c.active = 'Active'
    AND p.status = 'Active' GROUP BY    
    CASE a.productid
        WHEN 6 THEN a.productid + space(2) + a.cusotmerid + p.progcode+ w.level 
        WHEN 7 THEN a.productid + space(1) + a.cusotmerid + p.progcode+ w.level
        ELSE a.productid + space(3) + a.cusotmerid + p.progcode + w.level
    END 
    ORDER BY
    aonBom