标准:
- 如果托盘中的所有SKU都有产品组LAMINAAT - > LAM
- 如果托盘中的所有SKU都有产品组ACCESSORIE - > ACC
- 如果它是LAMINAAT和ACCESSORIE的混合物 - > MIX
- 只要1个SKU有产品组≠LAMINAAT或ACCESSORIE - >将此字段留空
醇>
我当前的select语句的结果给出了以下结果:
ORDER_ID, PALLET_ID, SKU_ID, PRODUCT_GROUP
80081876 00257084646912345679 9410-3798 ACCESSORIE
80081876 00257084646912345679 9420-3012 ACCESSORIE
80081876 00257084646912345679 9410-3012 ACCESSORIE
80081876 00257084646912345679 9410-3010 ACCESSORIE
80081876 00257084646912345678 9420-3865 ACCESSORIE
80081876 00257084646912345678 9410-3857 ACCESSORIE
80081876 00257084646912345678 9420-3010 ACCESSORIE
80081876 00997084646920000197 3010-3798 LAMINAAT
80081876 00280120160000000001 9420-3853 ACCESSORIE
80081876 00280120160000000001 9420-3895 ACCESSORIE
80081876 00280120160000000001 9320-3853 LAMINAAT
80081876 00280120160000000001 9420-3798 ACCESSORIE
数据的结果集应该说:
Pallet id product group
00257084646912345679 ACC
00280120160000000001 MIX
00997084646920000197 LAM
我在考虑这段代码
WITH cte AS
(SELECT pallet_id,
COUNT(DISTINCT PRODUCT_GROUP) AS counter
FROM product_group_data
GROUP BY pallet_id
)
SELECT p.pallet_id,
p.PRODUCT_GROUP,
CASE WHEN c.counter = 1
THEN SUBSTR(p.PRODUCT_GROUP,1,3)
ELSE 'MIX'
END uniq
FROM product_group_data p
JOIN cte c
ON p.pallet_id = c.pallet_id
这涵盖了1,2,3的标准......遗憾的是不是4。
任何想法都更受欢迎
谢谢
答案 0 :(得分:1)
您可以使用条件聚合根据product_group
的成员分配新标签select pallet_id,
case
when count(case when product_group not in ('LAMINAAT','ACCESSORIE') then 1 end) > 0 then ''
when count(distinct product_group) > 1 then 'Mix'
when count(case when product_group = 'LAMINAAT' then 1 end) > 0 then 'LAM'
else 'ACC'
end as label
from mytable
group by pallet_id