SQL语句问题! ( 积分: 100 )

  • 主题发起人 主题发起人 angellover
  • 开始时间 开始时间
A

angellover

Unregistered / Unconfirmed
GUEST, unregistred user!
TABLE1表:
A1 B1 (金额) C1 ....
111 5 AA
222 3 BB
333 6 CC
.................
TABLE2表:
A2 B2(金额) C2
111 1 AA
333 1 CC
101(表1无) 1 AC2
.................
结果:
AA1 BB1 CC1
111 6 AA
222 3 BB
333 7 CC
.....................
-------------------------------------
有好想法都说说....谢谢!
有点左连接的味道!
 
Select A1 as AA1, Sum(B1) as BB1, C1 as CC1
From
(select A1, B1, C1 From Table1
union
select A2 as A1, B2 as B1, C2 as C1) T
Group by A1, C1
 
select a1, sum(b1), c1
from (select * from table1
union all
select * from table2) a
where exists (select 1 from table1 where a1 = a.a1)
group by a1,c1
 
ADOQuery1.First;
ADOQuery2.First;
for i:=0 to ADOQuery1.recordcount-1 do
begin
for j:=0 to ADOQuery2.recordcount-1 do
begin
if ADOQuery2.fieldbyname('A2').AsString=ADOQuery1.fieldbyname('A1').AsString then
begin
ADOQuery1.fieldbyname('B1').AsString:=ADOQuery1.fieldbyname('B1').AsString+ADOQuery2.fieldbyname('B2').AsString;
end;
ADOQuery2.next;
end;
ADOQuery1.next;
end;
 
补充还要没有101
Select A1 as AA1,
Sum(B1) as BB1,
C1 as CC1
From
(select A1, B1, C1 From Table1
union
select A2 as A1, B2 as B1, C2 as C1
from table2 where a2 in (select a1 from table1)) as T
Group by A1, C1
 
大概我表述的不是很完整,s00000000001比较准确,谢谢大家!
 
多人接受答案了。
 
后退
顶部