现在有一张表,记录为:
ID1 JE FY1 FY2
01 10 20 30
01 20 20 40
02 10 10 50
02 20 30 50
02 20 50 50
现在要求得到如下结果:
ID1 JE FY1 FY2
01 10 20 30
01 20 20 40
小计 30 40 70
02 10 10 50
02 20 30 50
02 20 50 50
小计 50 90 150
合计 80 130 220
即要明细与汇总
ID1 JE FY1 FY2
01 10 20 30
01 20 20 40
02 10 10 50
02 20 30 50
02 20 50 50
现在要求得到如下结果:
ID1 JE FY1 FY2
01 10 20 30
01 20 20 40
小计 30 40 70
02 10 10 50
02 20 30 50
02 20 50 50
小计 50 90 150
合计 80 130 220
即要明细与汇总
解决方案 »
- delphi连接mysql
- *** 高分请问一个比较难的问题,关于Delphi6和nvarchar的 ***
- 列举出某应用程序的主窗口以外的其它窗口
- 我刚学FastReport,哪位大哥有从Delphi调用FastReport的代码能给我一些么???
- 在vf數據庫中,delphi7一次怎麼只能copy 5000條紀錄,試過幾次都是5000條,關於delphi7數據庫的數據複製問題
- 急救!关于SQL语句,大鸟们帮帮忙!
- 动态建表
- Delphi 的一个很不科学的代码。。。求大神
- delphi 怎么获取他人程序的菜单按钮句柄。
- delphi中的ole控件为什么不能这样用???
- 一不小心多了一条裤衩!散分~~~~
- delphi程序员的工资有多少?
select ID1, JE, FY1, FY2 from table where id1='01'
union all
select '小计', sum(JE), sum(FY1), sum(FY2) from table where id1='01'
union all
select ID1, JE, FY1, FY2 from table where id1='02'
union all
select '小计', sum(JE), sum(FY1), sum(FY2) from table where id1='02'
Create Table 表(ID1 varchar(10),JE varchar(10),FY1 varchar(10),FY2 varchar(10))
--插入数据
insert into 表
select '01','10','20','30' union
select '01','20','20','40' union
select '02','10','10','50' union
select '02','20','30','50' union
select '02','20','50','50'
select * from 表
--测试语句
--以下ord用于排序
select ID1 as ord ,ID1, JE, FY1, FY2 from 表
union
select ID1 as ord, '小计' , sum(cast(JE as int)), sum(cast(FY1 as int)), sum(cast(FY2 as int)) from 表 group by id1
union
select '总计' as ord, '总计' , sum(cast(JE as int)), sum(cast(FY1 as int)), sum(cast(FY2 as int)) from 表
order by ord
--测试结果
ord ID1 JE FY1 FY2
---------- ---------- ----------- ----------- -----------
01 01 10 20 30
01 01 20 20 40
01 小计 30 40 70
02 02 10 10 50
02 02 20 30 50
02 02 20 50 50
02 小计 50 90 150
总计 总计 80 130 220
--删除测试环境
--Drop Table 表
union
select ID1 as ord, '小计' , sum(cast(JE as int)), sum(cast(FY1 as int)), sum(cast(FY2 as int)) from 表 group by id1
union
select '总计' as ord, '总计' , sum(cast(JE as int)), sum(cast(FY1 as int)), sum(cast(FY2 as int)) from 表
order by ord