Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
unCANny
Starting Member
2 Posts |
Posted - 2012-05-18 : 19:57:03
|
So to make this more interesting I have to cast strings to integers first in order to derive the "MEASUREMENT" column. Then I have to get the MAX value of the MEASUREMENT column for each ID, However in this scenario they are likely to be tied, so in that case I need to get the row with the latest DATE.SELECT ID, LEVEL, DATE, CAST(CASEWHEN LEVEL = 'high' THEN 3WHEN DETECTION = 'medium' THEN 2WHEN DETECTION = 'low' THEN 1END AS INT) AS MEASUREMENTFROM TABLEHere's an example tableID | DATE | MEASUREMENT | LEVEL______________________________________________________________________1 | 10/2/2010 | 2 | medium1 | 3/6/2011 | 2 | medium1 | 7/4/2010 | 1 | low2 | 3/6/2009 | 3 | high1 | 10/2/2010 | 2 | medium1 | 10/2/2010 | 1 | lowThe Output that I desire is :ID | DATE | MEASUREMENT | LEVEL______________________________________________________________________1 | 10/2/2010 | 2 | medium2 | 3/6/2009 | 3 | highI appreciate any help you can manage.Thanks |
|
unCANny
Starting Member
2 Posts |
Posted - 2012-05-18 : 20:39:51
|
I got it!SELECT DISTINCT ID, DATE, MEASUREMENT, LEVELFROM (SELECT RANK() OVER (PARTITION BY id ORDER BY MEASUREMENT desc, DATE) as rank_order, ID, DATE, MEASUREMENT, LEVELFROM TABLE ) aWHERE a.rank_order = 1 |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
|
|
|
|
|