select 月 as 月份,sum(保险1) as 应缴金额
     from TAB1 
     where 年 = '2004' and 月 <= '12' and 单位代码 = '01'
     group by 月

解决方案 »

  1.   

    你可以做成一个存储过程,用程序调用,显示用datagrid就OK了.
      

  2.   

    select a.月 as 月份,sum(a.保险) as 应缴金额
         from (select 年,1 as 月 ,单位代码,人,保险1 as 保险 from tab1
               union
               select 年,2 as 月 ,单位代码,人,保险2 from tab1
               ...
               union
               select 年,12 as 月 ,单位代码,人,保险12 from tab1) a
         where a.年 = '2004' and a.月 <= '12' and a.单位代码 = '01'
         group by a.月
      

  3.   

    如果要显示成这种类型的报表用sql语句是无法实现,一般情况是用水晶报表做的,或者自己做个报表控件也行的.
      

  4.   

    这个比较简单啊,这样的sql语句就能做成报表了,不知道是否还有其他要求?select 月,sum(保险1) as 应缴金额
         from TAB1 
         group by 月
      

  5.   

    libin_ftsafe(子陌红尘) 的方法看起来不错。我试试
      

  6.   

    --可以实现到这种效果(其他格式化的东西,我想应该在前台去处理)--测试数据
    create table tb(年 int,单位代码 varchar(10),人 varchar(10)
    ,bx1 decimal(10,2),bx2 decimal(10,2),bx3 decimal(10,2),bx4 decimal(10,2)
    ,bx5 decimal(10,2),bx6 decimal(10,2),bx7 decimal(10,2),bx8 decimal(10,2)
    ,bx9 decimal(10,2),bx10 decimal(10,2),bx11 decimal(10,2),bx12 decimal(10,2))
    insert tb select 2003,'01','萧微' ,69.00 ,68.00 ,69.00 ,68.00 ,69.00 ,68.00 ,69.00 ,68.00 ,69.00 ,68.00 ,69.00 ,68.00
    union all select 2003,'01','戴立' ,56.00 ,60.00 ,56.00 ,60.00 ,56.00 ,60.00 ,56.00 ,60.00 ,56.00 ,60.00 ,56.00 ,60.00
    union all select 2004,'01','萧微' ,56.00 ,60.00 ,56.00 ,60.00 ,56.00 ,60.00 ,56.00 ,60.00 ,56.00 ,60.00 ,56.00 ,60.00
    union all select 2004,'01','戴立' ,56.00 ,60.00 ,56.00 ,60.00 ,56.00 ,60.00 ,56.00 ,60.00 ,56.00 ,60.00 ,56.00 ,60.00
    go--查询的存储过程
    create proc p_qry
    @单位 varchar(10),
    @月份 int
    as
    if @月份 is null or @月份<1 or @月份>12 return --如果月份不符合要求,则退出declare @s Nvarchar(4000)
    select @s=''
    while @月份>0
    select @s=' union all select 年,月份='''+right(100+@月份,2)
    +''',bx=bx'+cast(@月份 as varchar)+' from tb where 单位代码=@单位'+@s
    ,@月份=@月份-1
    set @s='select 年,月份,应缴金额=sum(bx) from('+stuff(@s,1,11,'')+')a group by 年,月份 order by 年,月份'
    exec sp_executesql @s,N'@单位 varchar(10)',@单位
    go--调用
    exec p_qry '01',3
    go--删除测试
    drop table tb
    drop proc p_qry/*--测试结果年           月份   应缴金额       
    ----------- ---- -----------------
    2003        01   125.00
    2003        02   128.00
    2003        03   125.00
    2004        01   112.00
    2004        02   120.00
    2004        03   112.00(所影响的行数为 6 行)
    --*/
      

  7.   

    搞不明白楼主想怎么
    由于在工作中经常做这样的报表,所以比较熟,给个思路吧
    1,将要取的数据做成一个最终表
    2,3为将行转成列是转为几列,超过列数就会转行。
      select max(case when id%3=1 then td.年  else '' end) as 年1,
             max(case when id%3=1 then td.部门    else '' end) as 部门1,
             max(case when id%3=1 then td.金额 else '' end) as 金额1,
             max(case when id%3=2 then td.年  else '' end) as 年2,
             max(case when id%3=2 then td.部门    else '' end) as 部门2,
             max(case when id%3=2 then td.金额 else '' end) as 金额2,
             max(case when id%3=0 then td.年  else '' end) as 年3,
             max(case when id%3=0 then td.部门    else '' end) as 部门3,
             max(case when id%3=0 then td.金额 else '' end) as 金额3,
             (id-1)/3+1 as page
       from
        (select count(a.关键字段) as id,a.要取的字段,如年,部门,金额等
           from 最终表 as a,最终表 as b
           where a.关键字段>=b.关键字段
           gorup by a.要取的字段,如年,部门,金额等
        ) as td
    group by (id-1)/3+1
    order by (id-1)/3+1
      

  8.   

    我的问题已经解决了。采用的是libin_ftsafe(子陌红尘)的方法,谢谢各位的支持!!都有分相送!