比较相同的记录差异的不同日期

时间:2018-06-07 16:53:33

标签: sql sql-server

我在包含不同日期(D_DT)但相同key的表格中有记录。我的REC TYPE列有ACTVDISP。我正在尝试查找一个日期DISP的记录,并且在上一个日期为ACTVD_DT列位于季度,例如'2014-12-31','2013-09-30'等。我的合作伙伴提到了使用月末参数,但我不确定如何实现这一点。

感谢任何帮助。谢谢!

REC_TYPE       REC_KEY                         d_dt
DISP    CML0010000042001420000000000     2017-12-31 00:00:00.000
ACTV    CML0010000042001420000000000     2015-03-31 00:00:00.000
ACTV    CML0010000042001420000000000     2017-06-30 00:00:00.000
ACTV    CML0010000042001420000000000     2015-09-30 00:00:00.000

3 个答案:

答案 0 :(得分:2)

我仍然不能100%确定我理解您的预期结果,但如果我理解正确,可以选择使用exists

select *
from yourtable t1
where t1.rec_type = 'DISP' and
    exists (
       select 1
       from yourtable t2
       where t1.rec_key = t2.rec_key
          and t2.rec_type = 'ACTV'
          and t2.d_dt < t1.d_dt
    )

答案 1 :(得分:2)

因此,这将为您提供REC_TYPE从ACTV切换到DISP的每个密钥的第一个实例。它没有考虑月末边界,只是发生变化的第一个实例。

CREATE TABLE #temp (
    REC_TYPE NVARCHAR(4),
    REC_KEY NVARCHAR(50),
    d_dt DateTime
)

INSERT INTO #temp VALUES ('DISP','CML0010000042001420000000000',CAST('2018-02-28 00:00:00.000' AS DateTime))
INSERT INTO #temp VALUES ('DISP','CML0010000042001420000000000',CAST('2018-01-31 00:00:00.000' AS DateTime))
INSERT INTO #temp VALUES ('DISP','CML0010000042001420000000000',CAST('2017-12-31 00:00:00.000' AS DateTime))
INSERT INTO #temp VALUES ('ACTV','CML0010000042001420000000000',CAST('2015-03-31 00:00:00.000' AS DateTime))
INSERT INTO #temp VALUES ('ACTV','CML0010000042001420000000000',CAST('2017-06-30 00:00:00.000' AS DateTime))
INSERT INTO #temp VALUES ('ACTV','CML0010000042001420000000000',CAST('2015-09-30 00:00:00.000' AS DateTime))

INSERT INTO #temp VALUES ('DISP','CML0010000042001420000000001',CAST('2018-02-28 00:00:00.000' AS DateTime))
INSERT INTO #temp VALUES ('DISP','CML0010000042001420000000001',CAST('2018-01-31 00:00:00.000' AS DateTime))
INSERT INTO #temp VALUES ('DISP','CML0010000042001420000000001',CAST('2017-12-31 00:00:00.000' AS DateTime))
INSERT INTO #temp VALUES ('ACTV','CML0010000042001420000000001',CAST('2015-03-31 00:00:00.000' AS DateTime))
INSERT INTO #temp VALUES ('ACTV','CML0010000042001420000000001',CAST('2017-06-30 00:00:00.000' AS DateTime))
INSERT INTO #temp VALUES ('ACTV','CML0010000042001420000000001',CAST('2015-09-30 00:00:00.000' AS DateTime))

SELECT [T1].[REC_TYPE], [T1].[REC_KEY], MIN([T1].[d_dt]) as [d_dt]
FROM #temp AS [T1]
    LEFT JOIN #temp [T2]
        ON T2.[d_dt] = (SELECT TOP 1 [d_dt]
                        FROM #temp
                        WHERE REC_KEY = T1.REC_KEY
                        AND [REC_TYPE] = 'ACTV'
                        AND [d_dt] < T1.[d_dt]                        
                        ORDER BY [d_dt] ASC) 
WHERE T1.[REC_TYPE] = 'DISP'
GROUP BY [T1].[REC_TYPE], [T1].[REC_KEY]
ORDER BY MIN([T1].[d_dt]) ASC

答案 2 :(得分:2)

使用滞后

declare @t table (REC_TYPE varchar(20), REC_KEY varchar(40), d_dt date);
insert @t values 
('DISP', 'CML0010000042001420000000000', '2017-12-31 00:00:00.000'),
('ACTV', 'CML0010000042001420000000000', '2015-03-31 00:00:00.000'),
('ACTV', 'CML0010000042001420000000000', '2017-06-30 00:00:00.000'),
('ACTV', 'CML0010000042001420000000000', '2015-09-30 00:00:00.000');
select *
from ( select *
            , lag(REC_TYPE) over (order by d_dt) as lastREC_TYPE
          from @t t
     ) tt
where tt.REC_TYPE = 'DISP' and tt.lastREC_TYPE = 'ACTV' 
order by d_dt