try: select bh,name, kmmcl='aa', sum(case kmmc='aa' then cj else 0) as cj1, kmmcl='bb', sum(case kmmc='bb' then cj else 0) as cj2, kmmcl='cc', sum(case kmmc='cc' then cj else 0) as cj3, kmmcl='dd', sum(case kmmc='dd' then cj else 0) as cj4 from table group by bh,name
select aa.*, (select kmmc from table where kmmc='aa' and bh=aa.bh) kmmc1, (select cj from table where kmmc='aa' and bh=aa.bh) cj1, ......... from (select bh,name from tabel group by bh,name)AA
select bh,name,
kmmcl='aa', sum(case kmmc='aa' then cj else 0) as cj1,
kmmcl='bb', sum(case kmmc='bb' then cj else 0) as cj2,
kmmcl='cc', sum(case kmmc='cc' then cj else 0) as cj3,
kmmcl='dd', sum(case kmmc='dd' then cj else 0) as cj4
from table group by bh,name
(select kmmc from table where kmmc='aa' and bh=aa.bh) kmmc1,
(select cj from table where kmmc='aa' and bh=aa.bh) cj1,
.........
from
(select bh,name from tabel group by bh,name)AA