日期:2014-05-18  浏览次数:20679 次

求一条简单SQL语句,解决马上结贴
求一条简单SQL语句,情况如下

A表

id     j1
1       20
2       30
3       40

B表

id       j2
1         5
2         6
2         7
2         8
3         10
3         15

要的结果如下

id     j1     j2
1       20     5
2       30     21
3       40     25

虽是简单,单要求不能使用group   by,麻烦大家出手

------解决方案--------------------
declare @t1 table(id int, j1 int)
insert @t1
select 1, 20
union all select 2, 30
union all select 3, 40

declare @t2 table(id int, j2 int)
insert @t2
select 1, 5
union all select 2, 6
union all select 2, 7
union all select 2, 8
union all select 3, 10
union all select 3, 15

select a.id,a.j1,(select sum(j2) from @t2 where id = a.id ) as j2
from @t1 as a

/*结果
id j1 j2
------------------------
1 20 5
2 30 21
3 40 25
*/
------解决方案--------------------
也没有别的好答案了,结吧