How to add a row number within a group in my query

前端 未结 1 616
误落风尘
误落风尘 2021-01-13 23:17

I\'ve tried this query:

SELECT X,Y,Z,COUNT(*) 
FROM TABLE1 
GROUP BY X,Y,Z

and my result is:

相关标签:
1条回答
  • 2021-01-13 23:28

    This should do the trick:

    SELECT X,Y,Z,ROW_NUMBER() OVER (PARTITION BY X,Y,Z ORDER BY X,Y,Z)
    FROM TABLE1 
    

    The ROW_NUMBER() will tick up for every value in the group X,Y,Z, and reset at a next group. The ORDER BY clause is used to define in what order it should tick up, and can be changed to however you wish. This is one of the analytical functions Oracle provides, and can be very useful.

    0 讨论(0)
提交回复
热议问题