各位大大,请教sql语句

NormanZL 2010-11-09 03:49:54
sql server 2005

表1
id lastmonth
-------------------
1 12
2 13
3 24
5 15

表2
id nextmonth
-------------------
2 23
3 21
4 12

现在想得到这样一张表
id lastmonth nextmonth campare
--------------------------------
1 12 0 null
2 13 23 10
3 24 21 -3
4 0 12 null
5 15 0 null
...全文
119 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
luoyefeng1022 2010-11-09
  • 打赏
  • 举报
回复
联合+计算=搞定
fengyun142415 2010-11-09
  • 打赏
  • 举报
回复

SELECT ISNULL(a.id,b.id) id,
ISNULL(lastmonth,0) lastmonth,
ISNULL(nextmonth,0) nextmonth,
nextmonth-lastmonth campare
FROM 表1 a full join 表2 b ON a.id=b.id
order by id
tsinghua84 2010-11-09
  • 打赏
  • 举报
回复
SELECT ISNULL(a.id, b.id) AS id,
ISNULL(a.lastmonth, 0) AS lastmonth,
ISNULL(b.nextmonth, 0) AS nextmonth,
b.nextmonth -a.lastmonth AS campare
FROM T1 AS a
FULL JOIN T2 AS b
ON a.id = b.id
jwdream2008 2010-11-09
  • 打赏
  • 举报
回复
rucypli 2010-11-09
  • 打赏
  • 举报
回复
select tb1.id,isnull(tb1.lastmonth,0),isnull(tb2.nextmonth,0),tb2.nextmonth-tb1.lastmonth
from tb1 full outer join tb2 on tb1.id=tb2.id

playwarcraft 2010-11-09
  • 打赏
  • 举报
回复
select isnull(a.id,b.id) as id,
isnull(a.lastmonth,0) as lastmonth,
isnull(b.nextmonth,0) as nextmonth,
b.nextmonth-a.lastmonth as campare
from T1 as a
full join T2 as b
on a.id=b.id
--小F-- 2010-11-09
  • 打赏
  • 举报
回复
select
isnull(a.id.b.id) as id,isnull(a.lastmonth,0) as lastmonth,isnull(a.nextmonth,0) as nextmonth,isnull(a.lastmonth) -isnull(a.nextmonth)
from
表1 a
full join
表2 b
on
a.id=b.id
chuifengde 2010-11-09
  • 打赏
  • 举报
回复
SELECT COALESCE(a.id,b.id) id,
ISNULL(lastmonth,0) lastmonth,
ISNULL(nextmonth,0) nextmonth,
nextmonth-lastmonth campare
FROM 表1 a full join 表2 b
ON a.id=b.id

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧