有2个表Ta,TbTa:
ID Acout
111 1
22 18
332 9Tb:
ID Bcout
111 2
3 34其中 两表的ID值有相同值
我想得到的结果是,把两表的ID放在一列,如下:
ID Acout Bcout
111 1 2
22 18 null
332 9 null
3 null 34这样。。
ID Acout
111 1
22 18
332 9Tb:
ID Bcout
111 2
3 34其中 两表的ID值有相同值
我想得到的结果是,把两表的ID放在一列,如下:
ID Acout Bcout
111 1 2
22 18 null
332 9 null
3 null 34这样。。
(
select 111,1 union all
select 22,18 union all
select 332,9
)
,b (ID,Bcout) as
(
select 111,2 union all
select 3,34
)select isnull(a.id,b.id),a.Acout,b.Bcout
from a
full join b on a.id=b.id
from a
full join b on a.id=b.id
ID,Postil
1 ,null
1 ,null
2 ,null
2 ,dsf
3 ,null
3 ,null
3 ,null
(ID=1有2个Postil=null,2有1个,3个3个)Tb
ID,b_Postil
1 ,null
2 ,null
2 ,null(ID=1有1个b_Postil=null,2个2个)我现在要得到:ID,Postil,b_Postil
1 2 1
2 1 2
3 3 我最终想得到的结果是把 Postil和b_Postil相加,即
ID, Postil
1 3
2 3
3 3万分感谢。
ID,Postil
Tb
ID,b_Postil
语句:select ID,sum(case when postil is null then 1 else 0 end) Postil
from(
select ID,Postil from ta
union all
select ID,b_Postil from tb
) tmp
group by ID