表1:
工号   姓名
11      aaa
22      bbb
表2:
工号    金额     时间
11      12.01  2004-1
11      13     2004-1
22      55     2004-3
22      78     2004-1
实现的报表是:
工号  姓名    发生金额   
            1月    2月    3月  4月。
11    aaa   25.01   0     0    0。
22    bbb   78      0     55    0。  
用那些组件,怎么 实现?明天要交,就差这个了,急啊 
用quickreport作的,delphi自带的有,可时间紧啊 ,谢谢你们了

解决方案 »

  1.   

    大哥,就看你的了 ,我 和我同事的分都给你 
    qdmis123
      

  2.   

    select 工号,姓名,sum(decode(工号,aaa,2004-1,金额,0) 1月,
                     sum(decode(工号,aaa,2004-2,金额,0) 2月,
                     sum(decode(工号,aaa,2004-3,金额,0) 3月,
                     sum(decode(工号,aaa,2004-4,金额,0) 4月,
                     sum(decode(工号,aaa,2004-5,金额,0) 5月,
                     sum(decode(工号,aaa,2004-6,金额,0) 6月,
                     sum(decode(工号,aaa,2004-7,金额,0) 7月,
                     sum(decode(工号,aaa,2004-8,金额,0) 8月,
                     sum(decode(工号,aaa,2004-9,金额,0) 9月,
                     sum(decode(工号,aaa,2004-10,金额,0) 10月,
                     sum(decode(工号,aaa,2004-11,金额,0) 11月,
                     sum(decode(工号,aaa,2004-12,金额,0) 12月,
                     sum(decode(工号,bbb,2004-1,金额,0) 1月,
                     sum(decode(工号,bbb,2004-2,金额,0) 2月,
                     sum(decode(工号,bbb,2004-3,金额,0) 3月,
                     sum(decode(工号,bbb,2004-4,金额,0) 4月,
                     sum(decode(工号,bbb,2004-5,金额,0) 5月,
                     sum(decode(工号,bbb,2004-6,金额,0) 6月,
                     sum(decode(工号,bbb,2004-7,金额,0) 7月,
                     sum(decode(工号,bbb,2004-8,金额,0) 8月,
                     sum(decode(工号,bbb,2004-9,金额,0) 9月,
                     sum(decode(工号,bbb,2004-10,金额,0) 10月,
                     sum(decode(工号,bbb,2004-11,金额,0) 11月,
                     sum(decode(工号,bbb,2004-12,金额,0) 12月,
    from 表1,表2 where 表1.工号=表2.工号
    group by 工号,姓名
    以上的可以用循环来完成.
      

  3.   

    对不气,写写错了..把aaa,bbb改为11,22
      

  4.   

    在quickreport里怎么实现
    我是用一个table连到人员表上,再用一个query在消费表里查询
    人有4000多人,详细讲讲,谢谢
      

  5.   

    简单可以用循环来写.
    定义.
    stemp1:工号
    stemp2:日期.
    n:数
    query1.close;
    query1.clear;
    query1.sql.add('select select 工号,姓名,');
    while not query2.eof do//date控件获取时间段查出数据.
    begin
    query1.sql.add('sum(decode(工号,'+#39+stemp1+#39,+#39+stemp2+#39,金额,0) stemp2月;
      query2.next; 
    end;
     query2.close;
     query1.sql.add('from 表1,表2 where 表1.工号=表2.工号');
     query1.sql.add('group by 工号,姓名');
      

  6.   

    对了你用的是什么数据库?
    decode(字段名,字段内容,条件,返回值(真),0(假));
    在sum一下就是求和.
    如支持decode的话
    就可以查出这样一个表
    工号  姓名   1月    2月    3月  4月。            
    11    aaa   25.01   0     0    0。
    22    bbb   78      0     55    0。  
    用那些组件,怎么 实现?明天要交,就差这个了,急啊 
      

  7.   

    上面的stemp1和stemp2可以从query2中得到.也可以从你的table中得到.
      

  8.   

    谢谢你,最好用一个sql语句,可以吗?
      

  9.   

    select b.工号,a.姓名,
      (select sum(金额) from 表1 where 工号=b.工号 and 时间>='2004-01-01' and 时间<='2004-01-31') as 1月,
    ...
    ...
    ...
    from 表1 a,表2 b
    where a.工号=b.工号不知道是否正确,我的思路就是这样的。
    是不是可以考虑更改一下数据结构,改得好的话可以使得代码写起来更容易一些。
      

  10.   

    Sorry:
    (select sum(金额) from 表1 where 工号=b.工号 and 时间>='2004-01-01' and 时间<='2004-01-31') as 1月,应该是from 表2
      

  11.   

    那个就是一个SQL只是比较大~~因为你要按月排..所以一个月是一个.
      

  12.   

    select a.*,b.* from yb1 a,(
       select C1,sum(C4),sum(m1),sum(m2),sum(m3),sum(m4),sum(m5),sum(m6),sum(m7),
    sum(m8),sum(m9),sum(m10),sum(m11),sum(m12) from(
          select C1,C4,
           max(decode(to_char(D1, 'mm'),'01',C4,0)) m1,
           max(decode(to_char(D1, 'mm'),'02',C4,0)) m2,
           max(decode(to_char(D1, 'mm'),'03',C4,0)) m3,
           max(decode(to_char(D1, 'mm'),'04',C4,0)) m4,
           max(decode(to_char(D1, 'mm'),'05',C4,0)) m5,
           max(decode(to_char(D1, 'mm'),'06',C4,0)) m6,
           max(decode(to_char(D1, 'mm'),'07',C4,0)) m7,
           max(decode(to_char(D1, 'mm'),'08',C4,0)) m8,
           max(decode(to_char(D1, 'mm'),'09',C4,0)) m9,
           max(decode(to_char(D1, 'mm'),'10',C4,0)) m10,
           max(decode(to_char(D1, 'mm'),'11',C4,0)) m11,
           max(decode(to_char(D1, 'mm'),'12',C4,0)) m12
            from(
          select C1,sum(C4) as C4,D1
           from yb2 group by C1,D1)
          group by C1,C4
         )
        group by C1) b
         where a.C1=b.C1(+);
    ===========================================================