select cc.id,cc.a,cc.b,cc.c from
(select b.id,(select sum(c) from a where id=b.id and lx='A') A ,
(select sum(c) from a where id=b.id and lx='B') B,
(select sum(c) from a where id=b.id and lx='C') C
from a b)cc
group by cc.id,cc.a,cc.b,cc.c
程序这样
var
s,c:string;
begin
s:='select b.id,'
c:='(select sum(c) from a where id=b.id and lx=''%s'') %s ,'
Query1.Close;
Query1.Sql.Text:='select distinct lx from a';
Query1.Open;
while not Query1.Eof do
begin
s:=s+Format(c,[Query1.FieldByName('lx').AsString,
Query1.FieldByName('lx').AsString]);
Query1.Next;
end;
s:=copy(s,1,Length(s)-1);
s:=s+ ' from a b';
这样S最后得到的就是你要的SQL
语句应该这样
表A
create table A
(
ID NUMBER,
LX VARCHAR2(1),
C NUMBER
)
ID A B C
1 1 A 10
2 1 B 12
3 1 C 14
4 2 A 11
5 2 C 13
select b.id,(select sum(c) from a where id=b.id and lx='A') A ,
(select sum(c) from a where id=b.id and lx='B') B,
(select sum(c) from a where id=b.id and lx='C') C
from a b
select 编号,aa.A,bb.B,cc.C
from
( select 编号, A = sum(类型)
from T1
where 类型 = 'A'
group by 编号) as aa
( select 编号, B = sum(类型)
from T1
where 类型 = 'B'
group by 编号) as bb
( select 编号, C = sum(类型)
from T1
where 类型 = 'C'
group by 编号) as bb
where aa.编号 *=bb.编号 and aa.编号 *= cc.编号