Nacho DP Nacho DP - 1 month ago 7
SQL Question

Selecting non duplicates from distint query

I am fighting with an SQL query and I am not able to solve it. A have a table like this:

SELECT DISTINCT Field1, Field2
FROM myTable
WHERE Field3 = 'value';


ID Field1 Field2
1 Yes Info
1 No Info
2 Yes Info
3 No Info
3 Yes Info
4 No Info


I would like to have a resulting query where I don't have any duplicate values. In case of having duplicates values I would like to have a resulting 'Yes' and in other case I would like to have the existing value in that Field (Yes or No).

ID Field1 Field2
1 Yes Info
2 Yes Info
3 Yes Info
4 No Info


Thanks in advance!

Answer

If you want to preferentially choose Yes over No for a given pair of records corresponding to an ID, then you can choose the max of Field1 (and the max of Field2):

SELECT ID, MAX(Field1) AS Field1, MAX(Field2) AS Field2
FROM myTable
WHERE Field3 = 'value'
GROUP BY ID
Comments