现有 表 [费用清单] ,字段有 [号码] ,[费用] 因为一个号码会有多条记录
所以要求每个号码的合计费用,select * from [表头] ----(表头是要求用来给每个号码的费用合计做一个表头的说明)
union all
select [号码],sum([费用]) from [费用清单] where [号码]='11111' group by [号码] with rollup
union all
select * from [表头] ----(表头是要求用来给每个号码的费用合计做一个表头的说明)
union all
select [号码],sum([费用]) from [费用清单] where [号码]='22222' group by [号码] with rollup这样一直把 [费用清单] 里的号码给取完,得到一张表,要打到这样的效果是不是要用while 循环语句? 请教下各位大大!
所以要求每个号码的合计费用,select * from [表头] ----(表头是要求用来给每个号码的费用合计做一个表头的说明)
union all
select [号码],sum([费用]) from [费用清单] where [号码]='11111' group by [号码] with rollup
union all
select * from [表头] ----(表头是要求用来给每个号码的费用合计做一个表头的说明)
union all
select [号码],sum([费用]) from [费用清单] where [号码]='22222' group by [号码] with rollup这样一直把 [费用清单] 里的号码给取完,得到一张表,要打到这样的效果是不是要用while 循环语句? 请教下各位大大!
解决方案 »
- 数据库硬盘满了,想加个硬盘的问题
- SQL Server 2008中无法设置主体'sa'的凭据。(Microsft SQL Server,错误:15535)
- SQL2005已经安装好了,不过在安装过程中出现了个警告。
- 求个函数
- 有一个问题憋了好久了,不吐不快:SQLSERVER的GROUP BY和HAVING子句为什么不能用前面已定义的别名?
- 存储过程问题
- sql server 2000 服务管理器启动停止按钮灰色
- 一个关于bcp的问题
- 衔接服务器问题
- 问一个union的子句问题
- 高手请进,小弟跪求aod和触发器问题。。请高手帮忙!!!!!!!!! 请邹健,大力高手帮忙。。。
- if exists 中的表名为变量,如何写,请帖出语句
select * from [表头]
union all
select [号码],sum([费用]) from [费用清单] group by [号码] with rollup
如果是表头说明想要作为你表中的一条记录的话,可以采用你的实现方式!select * from [表头] ----(表头是要求用来给每个号码的费用合计做一个表头的说明)
union all
select [号码],sum([费用]) from [费用清单] where [号码]='11111' group by [号码] with rollup
union all
select * from [表头] ----(表头是要求用来给每个号码的费用合计做一个表头的说明)
union all
select [号码],sum([费用]) from [费用清单] where [号码]='22222' group by [号码] with rollup
不需要在SQL这边这么费劲.
create table fyqd(hm int,fy money)
insert into fyqd select 1,25.30
insert into fyqd select 1,26.30
insert into fyqd select 1,3.50
insert into fyqd select 1,5.80
insert into fyqd select 1,205.10
insert into fyqd select 1,247.60
insert into fyqd select 2,15.90
insert into fyqd select 2,225.10
go
select hm,fy from fyqd order by hm compute sum(fy) by hm
go
drop table fyqd
/*
hm fy
----------- ---------------------
1 25.30
1 26.30
1 3.50
1 5.80
1 205.10
1 247.60sum
---------------------
513.60hm fy
----------- ---------------------
2 15.90
2 225.10sum
---------------------
241.00
(10 行受影响)
*/
union all
select [号码],sum([费用]) from [费用清单] group by [号码] with rollup
哦?使用啥报表工具?水晶报表?-------------------------------------------------其实有的大大没有明白我的意思哦,我主要是想最终得到一个这样的结果:
号码 费用
111 50
号码 费用
222 30
号码 费用
333 89
........
....
后面以此类推.
虽然有点象是 qianjin036a 大大的,但是我需要的结果在一张表中显示出来
select [号码],sum([费用]) from [费用清单] group by [号码] with rollup 子
这个记录集,每条记录之前 print '表头'...是不是这个意思啊?