我有以下结果集:
req_id | p_id | ai_result | hash | sku
1 | 4 | Match | XAN | HOW
1 | 4 | Match | HXN | HOW
1 | 4 | Non Match | 123 | HOW
我需要以下输出
sku | matched | non_matched
HOW | XAN, HXN | 123
据我所知:
SELECT sku, GROUP_CONCAT(hash) AS hash
FROM `sku_match`
GROUP BY req_id, p_id
如何区分基于ai_result
列的行并将它们分开放置。
像GROUP_CONCAT(hash) AS matched, GROUP_CONCAT(hash) AS non_matched
一样?
答案 0 :(得分:5)
尝试使用条件聚合:
SELECT
sku,
GROUP_CONCAT(CASE WHEN ai_result = 'Match' THEN hash END) AS matched,
GROUP_CONCAT(CASE WHEN ai_result = 'Non Match' THEN hash END) AS non_matched
FROM sku_match
GROUP BY
sku;