How to return a incremental group number per group in SQL

前端 未结 2 1735
鱼传尺愫
鱼传尺愫 2020-12-01 02:52

I would like create a data query in SQL to incrementally number groups of rows, grouped on a common datetime and keep the \"group numbers\" incrementing on the next datetime

相关标签:
2条回答
  • 2020-12-01 03:07

    you shouldn't be using ROW_NUMBER(),

    • use DENSE_RANK() instead
    • remove PARTITION BY

    query,

    SELECT hl.ts_DateTime,  
           hl.Tagname as [ID],  
           hl.TagValue as [Value],
           DENSE_RANK() OVER (ORDER BY ts_datetime) AS RowFilter
    FROM   Table1 hl 
    ORDER  BY RowFilter
    
    • SQLFiddle Demo
    0 讨论(0)
  • 2020-12-01 03:13

    I think you are looking for this:

    ROW_NUMBER() OVER (PARTITION BY hl.id ORDER BY hl.ts_DateTime) AS RowFilter
    
    0 讨论(0)
提交回复
热议问题