I got a table like this
AMID Entry ------- --------- 1000 MARS 1001 JUPITER 1002 SATURN 1003 VENUS 1003 SATURN 1004 NEPTUNE 1004 SATURN 1005 JUPITER 1005 MARS
Now I want to extract the DISTINCT AMID values with particular ENTRY values.
The end table should be like this
AMID Entry -------- -------- 1000 MARS 1001 JUPITER 1002 SATURN 1003 VENUS 1004 SATURN 1005 MARS
The condition to select ENTRY values is,
It has to take the values based on this priority :
- VENUS
- MARS
- JUPITER
- SATURN
- NEPTUNE
So If any AMID has both VENUS and MARS, it should take VENUS
MARS and SATURN, it should take MARS
NEPTUNE and JUPITER, it should take JUPITER.(select by priority).
You can do it with ranking functions (if you’re using SQL Server 2008):