我有2个表A,B
A
------
id A_count
1 10
1 10
2 20
2 20
B
------
id B_count
1 10
2 15
------
我的想法是用一条sql语句实现这样的功能,如下表
------
id a_count b_count
1 20 10
2 40 15
------
我的意思就是这样,但是我使用join,然后sum,始终不对,请教一下大家。
谢谢,先给100分,不够再给
A
------
id A_count
1 10
1 10
2 20
2 20
B
------
id B_count
1 10
2 15
------
我的想法是用一条sql语句实现这样的功能,如下表
------
id a_count b_count
1 20 10
2 40 15
------
我的意思就是这样,但是我使用join,然后sum,始终不对,请教一下大家。
谢谢,先给100分,不够再给
Declare @t table(id int,A_count int)
insert @t select 1 ,10
union all select 1,10
union all select 2,20
union all select 2,20
-----table2
declare @t1 table(id int,B_count int)
insert @t1 select 1,10
union all select 2,15
--SQL
select A.id,A.a_count,B.B_count from
(
select id,sum(a_count) as A_count
from @t group by id) A
inner join @t1 B
on A.id=B.id
http://community.csdn.net/Expert/topic/5160/5160963.xml?temp=.4844477
from a left join b on a.id = b.id
groubp by a.id
--------------
连接问题.
对于这种情况,一般是先汇总后再与第二张表相连,更清晰一些!