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:
I want the result to be like that if i select row with id 1 and 2
and if i select row with id 1,2 and 3
Thank's in advance
You could try using group_concat(distinct() )
SELECT GROUP_CONCAT(DISTINCT(firstname) SEPARATOR ' ') ,
GROUP_CONCAT(DISTINCT(lastname) SEPARATOR ' ')
FROM table
You can use a combination of CASE WHEN
and 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.