现有 表 [费用清单] ,字段有 [号码] ,[费用] 因为一个号码会有多条记录
所以要求每个号码的合计费用,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 循环语句? 请教下各位大大!

解决方案 »

  1.   

     --这样?
    select * from [表头] 
    union all 
    select [号码],sum([费用]) from [费用清单] group by [号码]  with rollup 
      

  2.   


    如果是表头说明想要作为你表中的一条记录的话,可以采用你的实现方式!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 
      

  3.   

    这个使用报表做很容易吧,group显式表头,
    不需要在SQL这边这么费劲.
      

  4.   

    要这种效果?
    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 行受影响)
    */
      

  5.   

    select [号码]  as  号码 ,sum([费用]) as  费用  from [费用清单] group by [号码] 
      

  6.   

    select * from [表头] 
    union all 
    select [号码],sum([费用]) from [费用清单] group by [号码]  with rollup 
      

  7.   


    哦?使用啥报表工具?水晶报表?-------------------------------------------------其实有的大大没有明白我的意思哦,我主要是想最终得到一个这样的结果:
    号码   费用
    111     50
    号码   费用
    222    30
    号码   费用
    333    89
    ........
    ....
    后面以此类推.
    虽然有点象是 qianjin036a 大大的,但是我需要的结果在一张表中显示出来
      

  8.   

    水晶报表和SQL Server的报表服务都可以实现.
      

  9.   

      就是用游标遍历
    select [号码],sum([费用]) from [费用清单] group by [号码]  with rollup 子
      这个记录集,每条记录之前 print '表头'...是不是这个意思啊?
      

  10.   

    嗯,我也知道,如果在客户端应用程序实现应该很简单.我用的是SQL SERVER 2K,哎,没办法,我在一个大企业下的分公司,但是企业的系统不完善,我只能取得服务器端的数据,然后自己使用SQL来对数据进行统计,取目标数据等等.我之前使用ASP+数据库,然后在ASP中循环是可以实现这样的功能的.....不过一些报表工具我还真没有用过.