我想在SQL中创建一个数据查询,对具有相同日期时间的行组进行递增编号,并保持“组号”在下一个日期时间上递增,以此类推。当使用partition by语句时,这些“组号”不得为每个组重置。以下是我的示例数据:
ts_DateTime |ID |Value|RowFilter|RequiredResult
--------------------------
2013/01/09 09:23:16 |8009 |0 |1 |1
2013/01/09 09:23:16 |8010 |0 |2 |1
2013/01/09 09:23:16 |8026 |0 |3 |1
2013/01/09 09:23:22 |8026 |0 |1 |2
2013/01/09 09:23:28 |8009 |0 |1 |3
2013/01/09 09:23:28 |8010 |0 |2 |3
2013/01/09 09:23:28 |8026 |0 |3 |3
2013/01/09 09:27:03 |8009 |0 |1 |4
2013/01/09 09:27:03 |8010 |0 |2 |4
2013/01/09 09:27:03 |8026 |0 |3 |4
2013/01/09 09:27:09 |8009 |0 |1 |5
2013/01/09 09:27:09 |8010 |0 |2 |5
2013/01/09 09:27:09 |8026 |0 |3 |5
2013/01/09 09:27:15 |8009 |0 |1 |6
2013/01/09 09:27:15 |8010 |0 |2 |6
2013/01/09 09:27:15 |8026 |0 |3 |6
我用来获取这些结果的查询是:
select hl.ts_DateTime, hl.Tagname as [ID], hl.TagValue as [Value],
ROW_NUMBER() OVER (PARTITION BY hl.ts_datetime ORDER BY hl.tagname) AS RowFilter
from Table1 hl
所以基本上,查看ts_DateTime
分区的RowFilter列,我会得到唯一的ROW编号。我实际需要的是,对于每个ts_DateTime
分区,RowFilter列应该像所需结果列一样。
PARTITION BY
子句作为第二个窗口函数(未分区)的ORDER BY
子句。 - sayap