TSQL语法在两列组上递增

时间:2018-01-28 14:07:46

标签: sql sql-server

我需要语法帮助才能根据2列的分组来增加列。

以下显示了所需的输出,其中我想显示按DateTime

分割的每个ID的增量

enter image description here

我尝试了以下内容但没有成功

SELECT hl.ts_DateTime,  
       hl.Tagname as [ID],  
       hl.TagValue as [Value],
       DENSE_RANK() OVER (ORDER BY hl.Tagname, ts_datetime) AS RowFilter
FROM   Table1 hl 
ORDER  BY hl.Tagname,  hl.ts_DateTime


SELECT hl.ts_DateTime,  
       hl.Tagname as [ID],  
       hl.TagValue as [Value],
       ROW_NUMBER() OVER (PARTITION BY ts_DateTime, Tagname  ORDER BY ts_DateTime ASC)  AS RowFilter
FROM   Table1 hl 
ORDER  BY hl.Tagname,  hl.ts_DateTime

示例sqlfiddle => http://sqlfiddle.com/#!18/131c8/1

1 个答案:

答案 0 :(得分:1)

SELECT hl.ts_DateTime,  
       hl.Tagname as [ID],  
       hl.TagValue as [Value],
       DENSE_RANK() OVER (PARTITION BY hl.Tagname ORDER BY hl.ts_DateTime) AS RowFilter
FROM   Table1 hl 
ORDER  BY hl.Tagname,  hl.ts_DateTime