日期:2014-05-17 浏览次数:20679 次
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (a nvarchar(4),b nvarchar(4),c nvarchar(4),d nvarchar(4),Year int,amount int,amountbefore sql_variant)
insert into [TB]
select 'aa','bb','cc','dd',2012,2000,null union all
select 'aa','bb','cc','dd',2013,5000,null
select * from [TB]
SELECT A.a,A.b,A.c,A.d,A.YEAR,a.amount,b.amount
FROM dbo.TB A
left JOIN TB B ON A.a = b.a AND a.b= b.b AND a.c = b.c AND a.d = b.d AND a.YEAR -1 = b.YEAR
/*
a b c d YEAR amount amount
aa bb cc dd 2012 2000 NULL
aa bb cc dd 2013 5000 2000*/