--先忽略时间字段问题,给你写个样例
--测试环境
declare @table1 table
(
id1 int,
id2 int,
amount int
)
insert into @table1
select 1,1,3 union all
select 1,2,4 union all
select 1,3,1 union all
select 2,1,8 union all
select 2,2,5
declare @table2 table
(
id1 int,
id2 int,
amount int
)
insert into @table2
select 1,1,3 union all
select 1,2,4 union all
select 1,4,9 union all
select 2,1,3 union all
select 2,3,10
--检测测试环境
select * from @table1
select * from @table2
--查询
select
isnull(a.id1,b.id1) as id1,
isnull(a.id2,b.id2) as id2,
isnull(a.amount,0)+isnull(b.amount,0) as TotalAmount
from
@table1 a full join @table2 b
on
a.id1=b.id1
and a.id2=b.id2
order by id1,id2
/*
id1 id2 amount
----------- ----------- -----------
1 1 3
1 2 4
1 3 1
2 1 8
2 2 5
id1 id2 amount
----------- ----------- -----------
1 1 3
1 2 4
1 4 9
2 1 3
2 3 10
查询结果如下,自行校验
id1 id2 TotalAmount
----------- ----------- -----------
1 1 6
1 2 8
1 3 1
1 4 9
2 1 11
2 2 5
2 3 10
*/