Let's say there is a table with two columns, First Name and Eye Color.
In this table you could potentially have:
Name - Green Eyes
Name - Brown Eyes
I'm trying to select all the rows for names with green eyes, but if there is a name that doesn't have green eyes, then I would like to select the name with Brown eyes. I never want to return two rows for a given name. Any thoughts on how to do this? Any help would be greatly appreciated!
SELECT COALESCE(g.name, b.name) name,
COALESCE(g.eye_color, b.eye_color) eye_color
FROM (
SELECT DISTINCT name, eye_color
FROM eye_colors
WHERE eye_color = 'green'
) g
FULL OUTER JOIN (
SELECT DISTINCT name, eye_color
FROM eye_colors
WHERE eye_color = 'brown'
) b
ON b.name = g.name
IF EXISTS(SELECT * FROM Table WHERE Color='Green')
SELECT * FROM Table WHERE Color = 'Green'
ELSE
SELECT * FROM Table WHERE Color='Brown'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With