Search code examples
sqlrdbms

How below column transform into double column with ascending group(sql)


I try to get this in single query but failed. Do we need to use group set?

colA
200
400
300
500
200

transform into

colA colB
200 1
300 2
300 2
400 3
500 4

Solution

  • Maybe use DENSE_RANK() windowing function if available in your SQL database

    like

    select ColA, 
    DENSE_RANK() OVER( order by ColA) as colB
    from yourtable