SQL(Teradata):删除由GroupBy / CASE语句

时间:2018-01-31 16:42:32

标签: sql group-by null case teradata

我的结果中出现NULL的问题。这是因为我正在使用我的Group By&带有“ItemDamagedStatus”的CASE语句。一种解决方案可能是打破这些CASE Statement项并对同一个表进行JOIN。但是,当我这样做时,一些数据被删除了。

下面的查询实际上给了我正确的数字。我只想根据以下内容汇总到一行:Product / Market / Group1。

思考?有问题吗?

SELECT   t1.Product
        , t1.Market 
        , t1.Group1                                            
        , COUNT(DISTINCT t1.ItemID ||'-'||t1.Date1) AS StoredMth
        , CASE WHEN t1.ItemDamagedStatus = 'C' THEN COUNT(DISTINCT t1.ItemID ||'-'|| t1.Date1) END AS CompleteDmgMth
        , CASE WHEN t1.ItemDamagedStatus = 'P' THEN COUNT(DISTINCT t1.ItemID ||'-'|| t1.Date1) END AS PartialDmgMth
        , CASE WHEN t1.ItemDamagedStatus = 'N' THEN COUNT(DISTINCT t1.ItemID ||'-'|| t1.Date1) END AS NotDmgMth
        , CASE WHEN t1.ItemRepairStatus = 'Y' THEN COUNT(DISTINCT t1.ItemID ||'-'|| t1.Date1) END AS RepairMth
FROM  MainDatabase.Items t1
WHERE  t1.Date1 BETWEEN '2017-01-01' AND '2017-12-31'
GROUP BY      t1.Product
            , t1.Market 
            , t1.Group1  
            , t1.ItemDamagedStatus
            , t1.ItemRepairStatus

我得到的结果:

Product Market Group1 StoredMth CompleteDmgMth PartialDmgMth NotDmgMth  RepairMth
Car     North  Y      950       50             NULL          NULL       75
Car     North  Y      NULL      NULL           100           NULL       NULL
Car     North  Y      NULL      NULL           NULL          800        NULL
Car     North  N      165       NULL           75            NULL       10
Car     North  N      NULL      NULL           NULL          90         NULL
Car     South  Y      1400      500            NULL          NULL       800
Car     South  Y      NULL      NULL           NULL          900        NULL

我想要的结果:

Product Market Group1 StoredMth CompleteDmgMth PartialDmgMth NotDmgMth  RepairMth
Car     North  Y      950       50             100           800        75
Car     North  N      165       NULL           75            90         10
Car     South  Y      1400      500            NULL          900        800

(只是一个跟进以防万一会抛出任何人或他们试图合并一些值...是:CompleteDmgMth + PartialDmgMth + NotDmgMth = StoredMth,但它在我们的数据中并不是非常准确所以我们使用两个不同的方法。)

如果有些内容看起来很奇怪或框架不正确,我很抱歉,这是我第一次在这里发帖。

2 个答案:

答案 0 :(得分:2)

使用聚合,但不是所有列。您可以将CASE表达式嵌套在COUNT(DISTINCT)

SELECT t1.Product, t1.Market, t1.Group1,                                        
        COUNT(DISTINCT t1.ItemID || '-' || t1.Date1) AS StoredMth
        COUNT(DISTINCT CASE WHEN t1.ItemDamagedStatus = 'C' THEN t1.ItemID || '' || t1.Date1) END) AS CompleteDmgMth
        COUNT(DISTINCT CASE WHEN t1.ItemDamagedStatus = 'P' THEN t1.ItemID || '' || t1.Date1 END) AS PartialDmgMth
        COUNT(DISTINCT CASE WHEN t1.ItemDamagedStatus = 'N' THEN t1.ItemID || '-' || t1.Date1 END) AS NotDmgMth
        COUNT(DISTINCT CASE WHEN t1.ItemRepairStatus = 'Y' THEN t1.ItemID || '-' || t1.Date1 END) AS RepairMth
FROM  MainDatabase.Items t1
WHERE  t1.Date1 BETWEEN '2017-01-01' AND '2017-12-31'
GROUP BY t1.Product, t1.Market, t1.Group1;

答案 1 :(得分:0)

您可以使用MAX仅捕获非空值而不是在这些字段上进行分组:

SELECT derived_table.Product,
derived_table.Market
, derived_table.Group1
, MAX(derived_table.StoredMth) as StoredMth
, MAX(derived_table.CompleteDmgMth) as CompleteDmgMth
, MAX(derived_table.PartialDmgMth) as PartialDmgMth
, MAX(derived_table.NotDmgMth) as NotDmgMth
, MAX(derived_table.RepairMth) as RepairMth
FROM (

SELECT   t1.Product
        , t1.Market 
        , t1.Group1                                            
        , COUNT(DISTINCT t1.ItemID ||'-'||t1.Date1) AS StoredMth
        , CASE WHEN t1.ItemDamagedStatus = 'C' THEN COUNT(DISTINCT t1.ItemID ||'-'|| t1.Date1) END AS CompleteDmgMth
        , CASE WHEN t1.ItemDamagedStatus = 'P' THEN COUNT(DISTINCT t1.ItemID ||'-'|| t1.Date1) END AS PartialDmgMth
        , CASE WHEN t1.ItemDamagedStatus = 'N' THEN COUNT(DISTINCT t1.ItemID ||'-'|| t1.Date1) END AS NotDmgMth
        , CASE WHEN t1.ItemRepairStatus = 'Y' THEN COUNT(DISTINCT t1.ItemID ||'-'|| t1.Date1) END AS RepairMth
FROM  MainDatabase.Items t1
WHERE  t1.Date1 BETWEEN '2017-01-01' AND '2017-12-31'
GROUP BY      t1.Product
            , t1.Market 
            , t1.Group1  
            , t1.ItemDamagedStatus
            , t1.ItemRepairStatus) as derived_table
GROUP BY derived_table.Product,
derived_table.Market
, derived_table.Group1;