Skip to content
Advertisement

Distinct sql based on condition More than two columns

Below is the table structure. Column one contains strings which might be present in more than one row. I want to write a query in such a way column1 value is only repeated once such that row where column2 contains A takes priority. If only B is available output the same. Also with this the related column3 needs to be printed. In cases where there are more than one row with same value in Column1 and Column2 pick any(Ex: row 2 and 3 in the below).

Sample Data is provided below and expected sql output.

Table Structure (there are other columns as well which is skipped here)

Column1 Column2 Column3
 123       A       1
 234       A       1
 234       A       4
 234       B       2
 435       A       2
 536       B       1

SQL Expected Output

Column1 Column2 Column3
 123       A      1
 234       A      1
 435       A      2
 536       B      1

Note : This is an extension to this question Distinct sql based on condition

Advertisement

Answer

You could try this:

SELECT   tab.Column1,
         tab.Column2,
         MIN(tab.Column3)
FROM     (SELECT Column1,
                 MIN(Column2) as min_column2
          FROM   tab
          GROUP BY Column1
         ) t
JOIN     tab
  ON     tab.Column1 = t.Column1
 AND     tab.Column2 = t.min_column2
GROUP BY tab.Column1,
         tab.Column2;

It’s plain ANSI-SQL and doesn’t depend on your DB or its version.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement