高手指教:如何写这样的SQL语句...

tccb 2003-01-08 01:04:52
有一个table,字段如下:
kmh(科目号),jdbz(借贷标志),je(金额)
能否通过一条或几条SQL语句得到以下输出:
kmh,借方笔数,借方总金额,贷方笔数,贷方总金额
...全文
60 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
tccb 2003-01-13
  • 打赏
  • 举报
回复
借方笔数为零时,借方总金额为零...
walonelee 2003-01-10
  • 打赏
  • 举报
回复
补充
oracle适用
walonelee 2003-01-10
  • 打赏
  • 举报
回复
select kmh"科目号",jdbz"类别",count(*)"笔数",sum(je)"总金额"
from table
group by kmh,jdbz;

tccb 2003-01-10
  • 打赏
  • 举报
回复
more ?
jackystar 2003-01-10
  • 打赏
  • 举报
回复
up
tccb 2003-01-09
  • 打赏
  • 举报
回复
try ...
DainelLee 2003-01-08
  • 打赏
  • 举报
回复

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
diruser 2003-01-08
  • 打赏
  • 举报
回复
select kmh,count(*),sum(je) from table where jdbz='借' group by kmh

select kmh,count(*),sum(je) from table where jdbz='贷' group by kmh
下面自己来吧
王集鹄 2003-01-08
  • 打赏
  • 举报
回复
/* 参考如下语句 */
/* 适用于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 科目号

2,497

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧