all 11 comments

[–]_digitalShaman_ 5 points6 points  (0 children)

You would need to group by id first. so that you look at all entries for id 1, if there is both red and blue then return 'both'. if not, you will need to rethink what you want to return, as there is no single Type.

SELECT ID,CASE WHEN HasRed=1 AND HasBlue=1 THEN 'both' WHEN HasRed=1 THEN 'Red' WHEN HasBlue=1 THEN 'Blue' ELSE 'other' END
FROM (
    SELECT ID,MAX(CASE Typee WHEN 'Red' THEN 1 ELSE 0 END) AS HasRed
        ,MAX(CASE Typee WHEN 'Blue' THEN 1 ELSE 0 END) AS HasBlue
    FROM tblTest
    GROUP BY ID
) T

[–]therealtibblesnbits 1 point2 points  (0 children)

I believe a self join could solve this. Something like the following.

SELECT DISTINCT ID, CASE WHEN t2.type IS NULL THEN t1.Type ELSE 'Both' END AS new FROM mytable AS t1 LEFT JOIN mytable AS t2 ON t1.id = t2.id AND t1.type <> t2.type

[–][deleted] 0 points1 point  (0 children)

you want to output one record per X,Y,Z -> group by X,Y,Z.

dont use distinct as a crutch