CREATE TABLE #im ( SId int, Reporting_Year_Id int );
GO
INSERT #im (SId, Reporting_Year_Id) VALUES (1,2011)
INSERT #im (SId, Reporting_Year_Id) VALUES (2,2011)
INSERT #im (SId, Reporting_Year_Id) VALUES (3,2011)
GO
CREATE TABLE #im1 ( SID int, PID int, Release int, Legal int, Seq int);
GO
INSERT #im1 (SID, PID, Release, Legal, Seq) VALUES (1,10005,1,18,1) --Trans_Type should be 'A'
INSERT #im1 (SID, PID, Release, Legal, Seq) VALUES (1,10005,1,10,1) --Trans_Type should be 'C'
INSERT #im1 (SID, PID, Release, Legal, Seq) VALUES (2,10005,1,18,1) --Trans_Type should be ' '
INSERT #im1 (SID, PID, Release, Legal, Seq) VALUES (3,10006,1,20,1) --Trans_Type should be 'A'
GO
declare @iCurrentFileId int
set @iCurrentFileId = 2;
WITH current_file as
(
select * from #im1 where SID = @iCurrentFileId
)
, previous_file as
(
select * from #im1 where SID = @iCurrentFileId -1
)
select c.SID, c.PID,c.Legal,c.Release,c.Seq,p.SID,
case when p.SID IS null then 'A'
else 'C' end as 'transaction_type'
from current_file c left outer join previous_file p on p.PID = c.PID and c.Seq = p.Seq
我需要将 transaction_Type 设置为:
'A' 如果除 SID 之外的所有记录都不存在于前一个 SID 中
'C' 如果除 SID 之外的所有记录都与前一个 SID 不同
' ' 如果所有记录都相同,除了 SID
怎么做?任何帮助将非常感激。谢谢。