how to show the latest data

  • Hi all, i know maybe its is really easy but I've been working on it for ages ! (

    Name Diploma year

    A 1991

    A 1986

    A 1981

    B 2001

    B 1996

    B 1992

    C 1990

    C

    C

    D 2006

    D

    D

    i have a 3-4 tables and join them, then it returns lots of people have diplomas from different

    school, but i need to show the last graduated school for everyone. like that

    A 1991

    B 2001

    C 1990

    D 2006

    .

    .

    .

    .

    .

    thanks for now ...

    Talha

  • From the example you've given, it would look like using MAX would suit you needs.

    However from the description you seem to need more and need to filter on rownumber()

    heres an example

    with cteGradYears(Person,Collage,YearGrad,RowN)(

    Select Person,Collage,YearGrad,row_number() over partition by(Person order by year desc)

    from GradTable

    )

    Select Person,Collage,YearGrad

    from cteGradYears

    where RowN = 1



    Clear Sky SQL
    My Blog[/url]

  • Thank you ! it gave an good idea ..

  • MAX works well in that situation, but depending on your indexes, TOP 1 with an ORDER BY can work consistently better. You might try that too.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • thanks for your interest )

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

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