--示例--测试数据
create table 表1(货物代码 varchar(10),类别 varchar(10),上期欠数 int,本期交数 int,累计欠数 int,年 int)
insert 表1 select '01','a',10,5 , 5 ,2003
union all select '01','c',10,10, 0 ,2003
union all select '02','a',10,5 , 5 ,2003
union all select '01','a',5 ,5 , 0 ,2004
union all select '01','c',0 ,10,-10,2004
union all select '02','a',5 ,5 , 0 ,2004create table 表2(货物代码 varchar(10),类别 varchar(10),本期交数 int,时间 datetime)
insert 表2 select '01','a',1 ,'2003-1-1'
union all select '01','c',1 ,'2003-1-1'
union all select '01','a',4 ,'2003-1-1'
union all select '01','c',9 ,'2003-1-1'
union all select '01','a',5 ,'2004-1-1'
union all select '01','c',10,'2004-1-1'
union all select '02','a',5 ,'2003-1-1'
union all select '02','a',5 ,'2004-1-1'
go--处理
select 检验结果=case
when a.货物代码 is null then '多了货物代码['+b.货物代码+']'
when b.货物代码 is null then '少了货物代码['+a.货物代码+']'
when a.类别 is null then '多了类别['+b.类别+']'
when b.类别 is null then '少了类别['+a.类别+']'
when a.上期欠数<>b.上期欠数 then '上期欠数不相等'
when a.本期交数<>b.本期交数 then '本期交数不相等'
when a.累计欠数<>b.累计欠数 then '累计欠数不相等'
end
,*
from(
select 货物代码,类别
,上期欠数=sum(上期欠数),本期交数=sum(本期交数),累计欠数=sum(累计欠数)
from(
select 货物代码,类别,上期欠数=0,本期交数=sum(本期交数),累计欠数=0
from 表2 where year(时间)=2004
group by 货物代码,类别
union all
select 货物代码,类别,累计欠数,0,累计欠数
from 表1 where 年=2003
)a group by 货物代码,类别
)a full join(
select 货物代码,类别,上期欠数,本期交数,累计欠数
from 表1 b where 年=2004
)b on a.货物代码=b.货物代码 and a.类别=b.类别
where a.货物代码 is null or b.货物代码 is null
or a.类别 is null or b.类别 is null
or a.上期欠数<>b.上期欠数
or a.本期交数<>b.本期交数
or a.累计欠数<>b.累计欠数
go--删除测试
drop table 表1,表2/*--测试结果(自己看)--*/
create table 表1(货物代码 varchar(10),类别 varchar(10),上期欠数 int,本期交数 int,累计欠数 int,年 int)
insert 表1 select '01','a',10,5 , 5 ,2003
union all select '01','c',10,10, 0 ,2003
union all select '02','a',10,5 , 5 ,2003
union all select '01','a',5 ,5 , 0 ,2004
union all select '01','c',0 ,10,-10,2004
union all select '02','a',5 ,5 , 0 ,2004create table 表2(货物代码 varchar(10),类别 varchar(10),本期交数 int,时间 datetime)
insert 表2 select '01','a',1 ,'2003-1-1'
union all select '01','c',1 ,'2003-1-1'
union all select '01','a',4 ,'2003-1-1'
union all select '01','c',9 ,'2003-1-1'
union all select '01','a',5 ,'2004-1-1'
union all select '01','c',10,'2004-1-1'
union all select '02','a',5 ,'2003-1-1'
union all select '02','a',5 ,'2004-1-1'
go--处理
select 检验结果=case
when a.货物代码 is null then '多了货物代码['+b.货物代码+']'
when b.货物代码 is null then '少了货物代码['+a.货物代码+']'
when a.类别 is null then '多了类别['+b.类别+']'
when b.类别 is null then '少了类别['+a.类别+']'
when a.上期欠数<>b.上期欠数 then '上期欠数不相等'
when a.本期交数<>b.本期交数 then '本期交数不相等'
when a.累计欠数<>b.累计欠数 then '累计欠数不相等'
end
,*
from(
select 货物代码,类别
,上期欠数=sum(上期欠数),本期交数=sum(本期交数),累计欠数=sum(累计欠数)
from(
select 货物代码,类别,上期欠数=0,本期交数=sum(本期交数),累计欠数=0
from 表2 where year(时间)=2004
group by 货物代码,类别
union all
select 货物代码,类别,累计欠数,0,累计欠数
from 表1 where 年=2003
)a group by 货物代码,类别
)a full join(
select 货物代码,类别,上期欠数,本期交数,累计欠数
from 表1 b where 年=2004
)b on a.货物代码=b.货物代码 and a.类别=b.类别
where a.货物代码 is null or b.货物代码 is null
or a.类别 is null or b.类别 is null
or a.上期欠数<>b.上期欠数
or a.本期交数<>b.本期交数
or a.累计欠数<>b.累计欠数
go--删除测试
drop table 表1,表2/*--测试结果(自己看)--*/
a.货物代码,
a.类别,
case
when (a.上期欠数 <> b.累计欠数) then '去年累计欠数与今年上期欠数不一致'
when (a.上期欠数 - c.本年交数 <> a.累计欠数) then '本年交数与本年累计欠数不一致'
else '数据正确'
end as 数据状态
from
表1 a
left join
表1 b
on
a.货物代码 = b.货物代码 and a.类别 = b.类别 and a.年 = 2004 and b.年 = 2003
left join
(select
货物代码,
类别,
sum(本期交数) as 本年交数
from
表2
where
year(时间) = 2004
group by
货物代码,类别) c
on
a.货物代码 = c.货物代码 and a.类别 = c.类别
libin_ftsafe(子陌红尘)出结果的方式我比较喜欢。。
先谢谢两位。。我先研究下。。看还有什么好方法没有。。
明天上午一起揭贴。。