Sql 如何在多个子记录集中查询不同的值

Sql 如何在多个子记录集中查询不同的值,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我正在使用SQLServer2008 下面是一个示例记录集 我想在每次更改时查询该值一次。例如,我想要第1、9、14、26、28、37和50行 我尝试过使用SELECT DISTINCT,但这不起作用,因为值列有重复的值,即第1行和第14行 我已经搜索了一段时间,没有找到解决方案。如果你有,请告诉我!谢谢 Row Value Time Stamp 1 1916 2013-10-01 00:05:00 2 1916 2013-10-01 00:06:00 3 1916

我正在使用SQLServer2008

下面是一个示例记录集

我想在每次更改时查询该值一次。例如,我想要第1、9、14、26、28、37和50行

我尝试过使用SELECT DISTINCT,但这不起作用,因为值列有重复的值,即第1行和第14行

我已经搜索了一段时间,没有找到解决方案。如果你有,请告诉我!谢谢

Row Value   Time Stamp
1   1916    2013-10-01 00:05:00
2   1916    2013-10-01 00:06:00
3   1916    2013-10-01 00:07:00
4   1916    2013-10-01 00:08:00
5   1916    2013-10-01 00:09:00
6   1916    2013-10-01 00:18:00
7   1916    2013-10-01 00:21:00
8   1916    2013-10-01 00:22:00
9   2272    2013-10-01 00:36:00
10  2272    2013-10-01 00:37:00
11  2272    2013-10-01 00:40:00
12  2272    2013-10-01 00:41:00
13  2272    2013-10-01 00:42:00
14  1916    2013-10-01 00:43:00
15  1916    2013-10-01 00:55:00
16  1916    2013-10-01 00:56:00
17  1916    2013-10-01 00:58:00
18  1916    2013-10-01 00:59:00
19  1916    2013-10-01 01:02:00
20  1916    2013-10-01 01:03:00
21  1916    2013-10-01 01:05:00
22  1916    2013-10-01 01:06:00
23  1916    2013-10-01 01:07:00
24  1916    2013-10-01 01:08:00
25  1916    2013-10-01 01:09:00
26  1860    2013-10-01 01:20:00
27  1860    2013-10-01 01:21:00
28  2272    2013-10-01 01:22:00
29  2272    2013-10-01 01:23:00
30  2272    2013-10-01 01:24:00
31  2272    2013-10-01 01:25:00
32  2272    2013-10-01 01:26:00
33  2272    2013-10-01 01:27:00
34  2272    2013-10-01 01:28:00
35  2272    2013-10-01 01:29:00
36  2272    2013-10-01 01:30:00
37  1917    2013-10-01 01:31:00
38  1917    2013-10-01 01:36:00
39  1917    2013-10-01 01:37:00
40  1917    2013-10-01 01:39:00
41  1917    2013-10-01 01:42:00
42  1917    2013-10-01 01:43:00
43  1917    2013-10-01 01:47:00
44  1917    2013-10-01 01:48:00
45  1917    2013-10-01 01:49:00
46  1917    2013-10-01 01:54:00
47  1917    2013-10-01 01:55:00
48  1917    2013-10-01 01:56:00
49  1917    2013-10-01 02:01:00
50  1860    2013-10-01 02:02:00
51  1860    2013-10-01 02:03:00
52  1860    2013-10-01 02:05:00
53  1860    2013-10-01 02:07:00
54  1860    2013-10-01 02:08:00

在最新版本的SQL Server中,可以使用lag函数。假设您有较旧的版本,则可以使用相关子查询:

select Value, TimeStamp
from (select t1.*,
             (select top 1 value
              from t t2
              where t2.timestamp < t.timestamp
              order by timestamp desc
             ) as prevvalue
      from t t1
     ) t1
where prevvalue is null or prevvalue <> value;

在最新版本的SQL Server中,可以使用lag函数。假设您有较旧的版本,则可以使用相关子查询:

select Value, TimeStamp
from (select t1.*,
             (select top 1 value
              from t t2
              where t2.timestamp < t.timestamp
              order by timestamp desc
             ) as prevvalue
      from t t1
     ) t1
where prevvalue is null or prevvalue <> value;

我显然还不能留下评论

where t2.timestamp < t.timestamp 
应该是

where t2.timestamp < t1.timestamp
上面使用的是公共表表达式

;with cte as (select t1.*,
             (select top 1 value
              from table1 t2
              where t2.timestamp < t1.timestamp
              order by timestamp desc
             ) as prevvalue
      from table1 t1
     ) 
select Value, TimeStamp
from cte
where prevvalue is null 
or prevvalue <> value

我显然还不能留下评论

where t2.timestamp < t.timestamp 
应该是

where t2.timestamp < t1.timestamp
上面使用的是公共表表达式

;with cte as (select t1.*,
             (select top 1 value
              from table1 t2
              where t2.timestamp < t1.timestamp
              order by timestamp desc
             ) as prevvalue
      from table1 t1
     ) 
select Value, TimeStamp
from cte
where prevvalue is null 
or prevvalue <> value

这只在2012年和更新版本的SQL Server中可用,但在2008年不可用…嗨,Gordon,我在实现这一点上遇到了困难。主要是因为为了获取上面发布的原始数据集,我已经使用了一个按[TimeStamp]按[TimeStamp]顺序按[TimeStamp]分区的行数作为num,以便每分钟仅获取1条记录。还有,有很多t。特别是,在t2.timestamp