1

我一直在尝试将查询中的数据扁平化为每个组的一行。我认为使用 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 语句是扁平化数据的方法,只是不确定我在这里做错了什么。

4

1 回答 1

0

从我所看到的,我会SUM从里面移动到外面,从里面删除最后 3 项GROUP BY并删除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
于 2013-08-10T15:00:07.730 回答