select distinct kmh,
(select count(*) from table where jdbz='借' and kmh=a.kmh) as 借方总数,
(select sum(je) from tabletable where jdbz='借' and kmh=a.kmh) as 借方总金额,
(select count(*) from table where jdbz='贷' and kmh=a.kmh) as 贷方总数,
(select sum(je) from tabletable where jdbz='借' and kmh=a.kmh) as 贷方总金额 from table a
/* 参考如下语句 */
/* 适用于SQL Server */
/* 以后问数据库问题的时,不要忘记告诉是什么类型 */
select 科目号,
sum(case 借贷标志 when '借方' then 1 else 0 end) as 借方笔数,
sum(case 借贷标志 when '借方' then 金额 else 0 end) as 借方总金额,
sum(case 借贷标志 when '贷方' then 1 else 0 end) as 贷方笔数,
sum(case 借贷标志 when '贷方' then 金额 else 0 end) as 贷方总金额
from 你的表名
group by 科目号