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)

SQL Expected Output

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

Advertisement

Answer

You could try this:

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