I have a table which for the same key, it will always have one record with value 'P' but sometimes that same key could have two records one with value 'P' and one with value 'L'; I need to create a VIEW that if both 'P' and'L' exist; it will return the record with 'L', ELSE, it will return record with value 'P';
ex1 has two records 'P' and 'L' -> only 'L' should be returned
ex2 has one record 'P' -> 'P' will be returned.
Best Answer
This answer assumes that your table
Ex1
has columns:key_col
— the key column.flag_col
— the flag column, containingL
orP
(with a check constraint).data_col
— a proxy for the unidentified set of columns containing the other data.key_col
andflag_col
.A view like this should work (I didn't say it would work fast):
This uses the fact that the flag column contains either
L
orP
andL
sorts beforeP
. So, if there are two rows for a given key, the valueL
will be selected, else the valueP
will be selected.