Need help in Query

  • Hi I need some help in writing the query

    ID DATEBEN

    19391227200412011

    193912272004120123

    193912272004120124

    [highlight=#ffff11]193912272009070122[/highlight]

    193933272004120122

    1939332720041201224

    193933272004120122

    1[highlight=#ffff11]93933272009080122[/highlight]

    There are 3 columns and ID ,date and Ben.I want to select only those IDs with recent DATE ...for example in the ID 19391227 the recent date is 20090701.....Help me in writing this logic....

    Thanks

    Sudheer

  • Do you mean you want the most recent date for each ID? If so, then Max(Date) and Group By ID should do what you need.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • I need rest of the columsn too......which are not same ......so i cannot do group by on those coloumns.......

  • ;with CTE (Row, ID, MostRecent, Ben) as

    (select row_number() over (partition by ID order by Date desc),

    ID,

    Date,

    Ben)

    select ID, MostRecent, Ben

    from CTE

    where Row = 1;

    Try that, see if it works.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • GSquared ... think you left out the FROM table clause

    ;with CTE (Row, ID, MostRecent, Ben) as

    (select row_number() over (partition by ID order by Date desc),

    ID,

    Date,

    Ben FROM yourtablesname)

    select ID, MostRecent, Ben

    from CTE

    where Row = 1;

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • Yep. Completely missed that one.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply