0

I have a following result set:

req_id |  p_id  | ai_result  |  hash | sku
1      |  4     | Match      |  XAN  | HOW
1      |  4     | Match      |  HXN  | HOW
1      |  4     | Non Match  |  123  | HOW

I need to have the following output

sku  | matched  |  non_matched
HOW  | XAN, HXN |  123

Here's as far as I could get:

SELECT sku, GROUP_CONCAT(hash) AS hash
FROM `sku_match` 
GROUP BY req_id, p_id

How can I distinguish rows based on ai_result column and put them separately. Something like GROUP_CONCAT(hash) AS matched, GROUP_CONCAT(hash) AS non_matched?

1
  • you can try subquery Commented Jun 19, 2019 at 5:32

1 Answer 1

5

Try using conditional aggregation:

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;

enter image description here

Demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.