select a.种类,sum(case when b.科目编码='aa' then 金额 else 0 end) aa
,sum(case when b.科目编码='bb' then 金额 else 0 end) bb
,sum(case when b.科目编码='cc' then 金额 else 0 end) cc
from tableC a
left join tableB b on a.区分编码=b.区分编码
left join tableA c on b.科目编码=c.科目编码
group by a.种类
,sum(case when b.科目编码='bb' then 金额 else 0 end) bb
,sum(case when b.科目编码='cc' then 金额 else 0 end) cc
from tableC a
left join tableB b on a.区分编码=b.区分编码
left join tableA c on b.科目编码=c.科目编码
group by a.种类
解决方案 »
- 按日期统计每天的数量,有什么办法让SQL语句短一些?
- SQL SERVER不能能实现能数据库访问的日志记录
- 请各位帮忙看下为什么我的sql2000不能启动?
- MSSQL 如何获取是从哪个存储过程或函数修改了表数据?
- 分布式数据中的INSERT,DELETE,SELECT语句怎么写!
- 急急急!如何写出这个查询语句?????????????????????????大哥,帮忙呀!!!!1
- windows验证,服务器windows密码和客户端windows密码必须一至才能访问数据库?谁能说说原理?
- access往sql server倒数据后的查询怎么办????
- TOP 后面可不可已带变量?请看下面代码
- 多级关联查询
- vs2005制作安装程序中选择了同时打包SQL Server,如何配置其安装参数?
- 图书管理系统数据库设计
from
(select a.种类,sum(case when b.科目编码='aa' then 金额 else 0 end) aa
,sum(case when b.科目编码='bb' then 金额 else 0 end) bb
,sum(case when b.科目编码='cc' then 金额 else 0 end) cc
from tableC a
left join tableB b on a.区分编码=b.区分编码
left join tableA c on b.科目编码=c.科目编码
group by a.种类 with rollup
)aa
order by 1
不过我统计的金额要多一个工序,要用sum计算得出,种类要变成日期
头痛ing.....
不过弄错了个条件。
tableb
收入支出 区分编码 科目编码
收入 001 aa
收入 002 bb
收入 003 cc
支出 001 aa
支出 002 bb
支出 003 cc
table c
种类 区分编码 金额 区分
0 001 100 收入
0 002 200 收入
0 003 300 支出
0 001 200 收入
0 002 300 收入
1 001 100 支出
1 002 200 收入
1 003 300 收入
1 001 200 收入
1 002 300 支出
2 001 100 支出
2 002 200 收入
2 003 300 收入
2 001 200 收入
2 002 300 支出
想要 结果
种类 收入支出 aa bb cc
0 收入 300 500 300
支出 200 100 100
利润 100 400 200
1 收入 300 500 300
支出 200 100 100
利润 100 400 200
2 收入 300 500 300
支出 200 100 100
利润 100 400 200
和计 收入 300 500 300
支出 200 100 100
利润 100 400 200
select case when a.种类 is null then '合计' else a.种类 end 种类
,sum(case when b.科目编码='aa' then 金额 else 0 end) aa
,sum(case when b.科目编码='bb' then 金额 else 0 end) bb
,sum(case when b.科目编码='cc' then 金额 else 0 end) cc
from tableC a
left join tableB b on a.区分编码=b.区分编码
left join tableA c on b.科目编码=c.科目编码
group by a.种类 with rollup
select a.种类,a.收入支出
,sum(case when b.科目编码='aa' then 金额 else 0 end) aa
,sum(case when b.科目编码='bb' then 金额 else 0 end) bb
,sum(case when b.科目编码='cc' then 金额 else 0 end) cc
from tableC a
left join tableB b on a.区分编码=b.区分编码
left join tableA c on b.科目编码=c.科目编码
group by a.种类,a.收入支出 with cube
order by a.种类,a.收入支出
见4楼的问题,谢谢了。
请楼主告诉我,你的结果怎么算出来的?我研究了一个小时,楞是没看出你的结果是怎么来的?而且种类0,1,2和合计的数据一模一样,只需要计算一个,然后生成个0,1,2和合计的表,交叉连接即可.貌似不需要什么合计或是rollup了.