SQL - 从上次重复发生状态获取

时间:2017-10-04 23:06:48

标签: sql teradata

我有一张如下表格

#ID ResultStatus    StatusDate
100 F               9/01/2017
100 S               6/01/2017
100 F               2/01/2017
300 F               7/01/2017
300 F               3/01/2017
300 S               1/01/2017
500 S               7/01/2017
800 F               7/01/2017
800 S               3/01/2017
800 F               2/01/2017
800 S               1/01/2017

我希望在最后一个'S'记录之后获得所有'F'记录。 它应该返回

  • 对于ID 100的9/01/2017记录
  • ID为3/01/2017和7/01/2017记录
  • 对于ID 500,因为没有F
  • ID 800的7/01/2017记录

    选择上次成功后的所有失败。

我正在使用Teradata SQL,但是非常感谢任何SQL帮助。

2 个答案:

答案 0 :(得分:1)

标准SQL方法是:

select t.*
from t
where t.resultstatus = 'F' and
      t.statusdate > (select max(t2.statusdate)
                      from t t2
                      where t2.resultstatus = 'S' and t2.id = t.id
                     );

但是,我也倾向于使用窗口函数来执行此操作:

select t.*
from (select t.*,
             max(case when t.resultstatus = 'S' then statusdate end) over (partition by id) as max_s
      from t
     ) t
where t.resultstatus = 'F' and
      t.statusdate > max_s;

如果您希望在没有S时显示所有行,请将where更改为:

where resultstatus = 'F' and
      (statusdate > max_s or max_s is null);

编辑:

以下内容也可以使用:

select t.*
from t
qualify t.resultstatus = 'F' and
        t.statusdate > max(case when t.resultstatus = 'S' then statusdate end) over (partition by id);

答案 1 :(得分:0)

通过使用CROSS JOIN,分析函数ROW_NUMBER(),我们可以解决这个问题。以下链接中的SQL解决方案将详细解释它。

DDL: -

CREATE TABLE Sample( ID INT, ResultStatus VARCHAR(10), StatusDate DATE);

INSERT INTO Sample VALUES(100,'F','09-01-2017');
INSERT INTO Sample VALUES(100,'S','06-01-2017');
INSERT INTO Sample VALUES(100,'F','02-01-2017');

INSERT INTO Sample VALUES(300,'F','07-01-2017');
INSERT INTO Sample VALUES(300,'F','03-01-2017');
INSERT INTO Sample VALUES(300,'S','01-01-2017');

INSERT INTO Sample VALUES(500,'F','07-01-2017');

INSERT INTO Sample VALUES(800,'F','07-01-2017');
INSERT INTO Sample VALUES(800,'S','03-01-2017');
INSERT INTO Sample VALUES(800,'F','02-01-2017');
INSERT INTO Sample VALUES(800,'S','01-01-2017');

SQL: -

SELECT B.id,B.ResultStatus,B.StatusDate
  FROM
(
SELECT *,
       ROW_NUMBER() OVER( PARTITION BY ID ORDER BY StatusDate DESC ) AS rn,
       ROW_NUMBER() OVER( PARTITION BY ID ORDER BY ResultStatus DESC,StatusDate ) AS rn_status
  FROM Sample ) A
  CROSS JOIN
 (
SELECT *,
       ROW_NUMBER() OVER( PARTITION BY ID ORDER BY StatusDate DESC ) AS rn,
       ROW_NUMBER() OVER( PARTITION BY ID ORDER BY ResultStatus DESC,StatusDate DESC ) AS rn_status
  FROM Sample 
 ) B
WHERE A.ResultStatus = 'S'
  AND A.ResultStatus != B.ResultStatus
  AND B.StatusDate > A.StatusDate 
  AND A.ID = B.ID
  AND A.rn > B.rn
  AND A.rn_status = 1
  AND B.rn_status - B.rn = 1 
;

http://sqlfiddle.com/#!6/b2d17/17