Select a.a0100,asl,bsl From (select a0100,count(*) asl from z where xm='a' group by a0100) a,(select a0100,count(*) bsl from z where xm='b' group by a0100) b where a.a0100=b.a0100
看看这个 //首先是数据jwcdb=# select * from aaa; f1 | f2 ------------+------------ 1 | A 1 | A 1 | B 2 | B 2 | B 2 | B 3 | A (7 rows)jwcdb=# select f1,count(*) as cnt,sum(case when f2='A' then 1 else 0 end ) as A,sum(case when f2='B' then 1 else 0 end ) as B from aaa group by f1 order by f1; f1 | cnt | a | b ------------+-----+---+--- 1 | 3 | 2 | 1 2 | 3 | 0 | 3 3 | 1 | 1 | 0(3 rows)不知道这个是不是你要的结果