当前位置: 代码迷 >> Sql Server >> SQL归拢查询
  详细解决方案

SQL归拢查询

热度:40   发布时间:2016-04-24 21:20:43.0
SQL合并查询
有2个表Ta,Tb

Ta:
ID Acout
111  1
22   18
332   9

Tb:
ID  Bcout
111   2
3    34

其中 两表的ID值有相同值
我想得到的结果是,把两表的ID放在一列,如下:
ID Acout Bcout
111  1     2
22   18    null
332  9     null
3    null  34

这样。。


------解决方案--------------------
select isnull(a.id,b.id) as id,a.Acout,b.Bcout
from a
full join b on a.id=b.id 
------解决方案--------------------
Ta
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
  相关解决方案