/* 参考如下语句 */ /* 适用于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 科目号
select kmh,count(*),sum(je) from table where jdbz='借' group by kmhselect kmh,count(*),sum(je) from table where jdbz='贷' group by kmh 下面自己来吧
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
select kmh"科目号",jdbz"类别",count(*)"笔数",sum(je)"总金额" from table group by kmh,jdbz;
/* 适用于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 科目号
下面自己来吧
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
from table
group by kmh,jdbz;