我想只隔离从'900'变为'RCF'的源键值。我的代码现在返回所有记录而不管任何更改。
select
PD_END_DT,
SOURCE_KEY_VALUE,
Fed_Class_cd
from XXXXXXXXXXXX
where (pd_end_dt = '2015-11-30' or PD_END_DT = '2015-12-31')
and (FED_CLASS_CD = '900' or FED_CLASS_CD = 'RCF')
示例数据:
pd_ned_dt Source_key_value Fed_Cls_Cd
2015-11-30 CML0100000565101565000000000 900
2015-11-30 CML0100000613059250000613000 900
2015-12-31 CML0100000613059250000613000 RCF
2015-12-31 CML0100000613060250000613000 RCF
2015-11-30 CML0100000613060250000613000 900
2015-11-30 CML0100000613061250000613000 900
2015-12-31 CML0100000613061250000613000 RCF
2015-12-31 CML0100000613062250000613000 RCF
2015-11-30 CML0100000613062250000613000 900
2015-11-30 CML0100000633001633000000000 900
2015-12-31 CML0100000633001633000000000 900
2015-12-31 CML0100000641001641000000000 RCF
2015-11-30 CML0100000641001641000000000 900
2015-11-30 CML0100000641002641000000000 900
2015-12-31 CML0100000641002641000000000 RCF
2015-12-31 CML0100000641003641000000000 RCF
我可以用什么查询来显示我想看到的记录?谢谢你的帮助!
答案 0 :(得分:0)
试试这个......
IF OBJECT_ID('tempdb..#TestData', 'U') IS NULL
BEGIN -- DROP TABLE #TestData;
CREATE TABLE #TestData (
pd_ned_dt DATE NOT NULL,
Source_key_value CHAR(28) NOT NULL,
Fed_Cls_Cd CHAR(3) NOT NULL
);
INSERT #TestData (pd_ned_dt, Source_key_value, Fed_Cls_Cd) VALUES
('2015-11-30', 'CML0100000565101565000000000', '900'),
('2015-11-30', 'CML0100000613059250000613000', '900'),
('2015-12-31', 'CML0100000613059250000613000', 'RCF'),
('2015-12-31', 'CML0100000613060250000613000', 'RCF'),
('2015-11-30', 'CML0100000613060250000613000', '900'),
('2015-11-30', 'CML0100000613061250000613000', '900'),
('2015-12-31', 'CML0100000613061250000613000', 'RCF'),
('2015-12-31', 'CML0100000613062250000613000', 'RCF'),
('2015-11-30', 'CML0100000613062250000613000', '900'),
('2015-11-30', 'CML0100000633001633000000000', '900'),
('2015-12-31', 'CML0100000633001633000000000', '900'),
('2015-12-31', 'CML0100000641001641000000000', 'RCF'),
('2015-11-30', 'CML0100000641001641000000000', '900'),
('2015-11-30', 'CML0100000641002641000000000', '900'),
('2015-12-31', 'CML0100000641002641000000000', 'RCF'),
('2015-12-31', 'CML0100000641003641000000000', 'RCF');
END;
--=============================================================
SELECT
ChangeDate = td1.pd_ned_dt,
td1.Source_key_value,
BeforeChange = td2.Fed_Cls_Cd,
AfterChange = td1.Fed_Cls_Cd
FROM
#TestData td1
JOIN #TestData td2
ON td1.Source_key_value = td2.Source_key_value
WHERE
td1.Fed_Cls_Cd = 'RCF'
AND td1.pd_ned_dt >= '2015-11-30'
AND td1.pd_ned_dt < '2016-01-01'
AND td2.Fed_Cls_Cd = '900'
AND td1.pd_ned_dt > td2.pd_ned_dt;
答案 1 :(得分:0)
select distinct pd_ned_dt , Source_key_value , Fed_Cls_Cd from xxx As a
where
Exists(select Source_key_value from xxx where Source_key_value=a.Source_key_value and Fed_Cls_Cd='900')
答案 2 :(得分:0)
这个怎么样?
SELECT *
FROM #XXXXXXXXXXXX q900
INNER JOIN #XXXXXXXXXXXX qRCF
ON q900.SOURCE_KEY_VALUE = qRCF.SOURCE_KEY_VALUE
AND q900.pd_end_dt < qRCF.pd_end_dt
AND q900.FED_CLASS_CD = '900'
AND qRCF.FED_CLASS_CD = 'RCF'
注意:如果PD_END_DT根本不相关,则需要一些其他方式来了解针对该密钥的RCF记录比900记录更新。可以用于其他任何领域吗?
答案 3 :(得分:0)
这就是你要找的吗?我不确定我是否理解你的问题..
SELECT
mt2.*
FROM
dbo.MyTable mt1
JOIN dbo.MyTable mt2
ON mt1.SOURCE_KEY_VALUE = mt2.SOURCE_KEY_VALUE
AND mt1.FED_CLASS_CD = 'RCF' // data that changed from RCF
AND mt2.FED_CLASS_CD = '900' //data that changed to 900
AND mt1.PD_END_DT < mt2.PD_END_DT // data that happened after, so updated
WHERE
AND mt1.PD_END_DT >= '2015-11-30'
AND mt1.PD_END_DT < '2016-01-01'
答案 4 :(得分:0)
尝试以下查询,只是您可以完成的想法。希望它能帮到你
;with testD
as
(
select *
, row_number() over(partition by Source_key_value order by pd_ned_dt) rowNumber
from XXXXXXXXXXXX
)
select prevData.*,currentData.*
from testD currentData left join testD prevData on
currentData.Source_key_value= prevData.Source_key_value and currentData.rowNumber=prevData.rowNumber+1
where prevData.Fed_Class_CD ='900'