How to get max of composite data in SQL?

Posted by Siddharth Sinha on Stack Overflow See other posts from Stack Overflow or by Siddharth Sinha
Published on 2012-10-25T10:55:15Z Indexed on 2012/10/25 11:00 UTC
Read the original article Hit count: 277

Filed under:
|
|
|
|
SELECT "Name""Month","Year","Value" 
from Table
WHERE 
    "Name" LIKE '%JERRY%'
AND "Year" = 
    (SELECT MAX("Year") FROM Table where "Name" LIKE '%JERRY%')
AND "Month"= 
    (SELECT MAX("Month") FROM Table 
     where 
        "Name" LIKE '%JERRY%' 
    AND "Year"= (SELECT MAX("Year") FROM Table where "Name" LIKE '%JERRY%'))

Table -->

Name  | Year | Month | Value
-----------------------------
JERRY   2012    9        100           
JERRY   2012    9        120         
JERRY   2012    9        130           
JERRY   2012    8         20        
JERRY   2011    12        50           

So i want the first three rows as output. As for the latest month for the latest year i need all the values. Can someone suggest a better cleaner query?

© Stack Overflow or respective owner

Related posts about sql

Related posts about query