一列是日期,另一列是状态,但我只需要在更改为状态日期后如何查找?

时间:2016-06-21 10:08:22

标签: sql-server

当前输入:

Date         Status
--------------------
6/20/2016    ABC
6/21/2016    ABC
6/22/2016    ABC
6/23/2016    DEF 
6/24/2016    ABC 
6/25/2016    ABC
6/26/2016    ABC
6/27/2016    ABC

期望的输出:

Date        Status 
-------------------
6/24/2016   ABC
6/25/2016   ABC
6/26/2016   ABC
6/27/2016   ABC

以上是我的意见和预期结果。什么日期后改变状态ABC持续,然后展示剩余变化,然后ABC不需要。

1 个答案:

答案 0 :(得分:1)

declare @t table (dt date,status varchar(3))
insert into @t values
('2016-06-20',   'ABC'),
('2016-06-21',   'ABC'),
('2016-06-22' ,  'ABC'),
('2016-06-23' ,  'DEF'),
('2016-06-24' ,  'ABC'),
('2016-06-25' ,  'ABC'),
('2016-06-26',   'ABC'),
('2016-06-27' ,  'ABC')

select * from @t
where   dt > (select max(dt) from @t where status <> 'ABC')