locked
sql query RRS feed

  • Question

  • User-974983964 posted

    I have a table like this:

    id   product
    1 10
    1 20
    1 30
    2 100
    2 55
    2 65
    3 70
    3 25
    3 40
    3 50

    Now I want to select top two of every id and desc order by product like this:

    id   product
    1 30
    1 20
    2 100
    2 65
    3 70
    3 50
    Tuesday, December 29, 2015 9:14 AM

Answers

  • User1724605321 posted

    Hi  Melody ,

    Try to use below code :

    with a as( select id,product,row_number() over(partition by id order by product desc) from tableName) 
    select * from a where a<3 

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 29, 2015 9:39 AM