For an example suppose I have a table with two columns X and Y. Now I want to select from this table some rows with the following rules:
- Define a “group” to be a group of rows that have same value of the column
Y. - From a group, only one row is chosen.
- In a group, if one of the rows has
X=awhere a is predefined, then that row is chosen from that group. - In a group, if no row has
X=a, then any row can be chosen.
What are my options to do this in a single SQL query?
Try this:
SQL Fiddle Demo