我遇到结果中显示 NULL 的问题。这是因为我如何使用我的Group By & CASE语句,"ItemDamageStatus"。一种解决方案是分解这些 CASE 语句项,并对同一表执行 JOIN。但是,当我这样做时,一些数据被丢弃了。
下面的查询是实际上给我正确数字的查询。我只想根据以下条件将其汇总到一行:产品/市场/组 1。
思潮?问题?
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 = StorageMth,但它在我们的数据中并不总是非常准确,所以我们使用两种不同的方法。
如果某些内容看起来很奇怪或框架不正确,我深表歉意,这是我第一次在这里发布。
使用聚合,但不对所有列使用聚合。 您可以将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;
您可以使用 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;