0

I have a sql table having multiple rows and i want to merge some of his rows to be in one row, if row columns different then return 'Mixed Value' else return the column value

Example:

SQL Table

I want the result to be like that if i select row with id 1 and 2

Table Resutls

and if i select row with id 1,2 and 3

Table Resutls

Thank's in advance

3

2 Answers 2

1

You could try using group_concat(distinct() )

SELECT GROUP_CONCAT(DISTINCT(firstname) SEPARATOR ' ') ,  
       GROUP_CONCAT(DISTINCT(lastname) SEPARATOR ' ')
FROM table
Sign up to request clarification or add additional context in comments.

Comments

0

You can use a combination of CASE WHENand COUNT(DISTINCT)to get what you want. It isn't clear if you want to group by price_id or by service_id, but I will assume so.

SELECT MIN(id) id, 
CASE WHEN COUNT(DISTINCT firstname) > 1 THEN 'MIXED_VALUE' ELSE MIN(firstname) END firstname,
CASE WHEN COUNT(DISTINCT lastname) > 1 THEN 'MIXED_VALUE' ELSE MIN(lastname) END lastname,
price_id, service_id,
CASE WHEN COUNT(DISTINCT product_sku) > 1 THEN 'MIXED_VALUE' ELSE MIN(product_sku) END product_sku
FROM yourTable
GROUP BY price_id, service_id

1 Comment

Why group by price_id, service_id? i want also to make the same thing on it

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.