(统计每周的数据,后面标头字段显示为当天以后每周星期一的日期,而数据则是统计它所在周的7天的request_qty数据,期前表示当天所在周以前所有周的数据之和,)当天是系统的当前日期吗?还是。。
-------------
code, codeName, 期前,6/1,6/8,6/15,6/22,6/27 要输出这么多列吗?包括6/1,6/8,6/15。
-------------
request_time(根据这个字段来判断是属于哪周)怎么判断?

解决方案 »

  1.   

    假设今天6/2, 星期二
    id, code, codeName, request_qty, request_time
    001 xxx   xxx       1             4/1
    002 xxx   xxx       2             6/3
    003 xxx   xxx       2             6/5
    004 xxx   xxx       3             6/10
    005 xxx   xxx       4             6/17006 yyy   yyy       1             4/1
    007 yyy   yyy       2             6/3
    008 yyy   yyy       3             6/10
    009 yyy   yyy       3             6/12
    010 yyy   yyy       4             6/17显示效果:(表头是星期一的日期,但内容是本周的数据之和,从当日所在周的星期一日期开始,一周一个字段,最多20个字段,之前都算期前的)
    code, codeName, 期前,6/1,6/8,6/15
    xxx   xxx        1     4     3    4
    yyy   yyy        1     2     6    4
      

  2.   

    说明:6/2不是星期二 而是星期四测试:SQL> select sysdate from dual;SYSDATE
    ----------
    02-6月 -05已用时间:  00: 00: 00.40
    SQL> select * from temp;ID         CODE       CODENAME   REQUEST_QTY REQUEST_TI
    ---------- ---------- ---------- ----------- ----------
    001        xxx        xxx                  1 4/1
    002        xxx        xxx                  2 6/3
    003        xxx        xxx                  2 6/5
    004        xxx        xxx                  3 6/10
    005        xxx        xxx                  4 6/17
    006        yyy        yyy                  1 4/1
    007        yyy        yyy                  2 6/3
    008        yyy        yyy                  3 6/10
    009        yyy        yyy                  3 6/12
    010        yyy        yyy                  4 6/17已选择10行。已用时间:  00: 00: 00.90
    SQL> select code,sum(decode(tr,0,request_qty,0)) a0,sum(decode(tr,1,request_qty,0)) a1,
      2  sum(decode(tr,2,request_qty,0)) a2,sum(decode(tr,3,request_qty,0)) a3,
      3  sum(decode(tr,4,request_qty,0)) a4
      4  from (
      5  select code,request_time,request_qty,decode(sign(tr),-1,-1,tr) tr from (
      6  select code,request_time,request_qty,trunc((
      7  to_date(request_time,'mm/dd')-(
      8  sysdate-decode(to_char(sysdate,'d'),1,7,to_char(sysdate,'d')-1)+1))/7) tr from temp
      9  ) t) tt group by code;CODE               A0         A1         A2         A3         A4
    ---------- ---------- ---------- ---------- ---------- ----------
    xxx                 4          3          4          0          0
    yyy                 2          6          4          0          0已用时间:  00: 00: 00.71
    SQL> 
    上面语句只写了5个周的,后面的自己加上就行了。
      

  3.   

    有期前的,刚才漏掉了
    为0 的 是 第一个周 即 当前周!=1的是下一个周的 以此类推
    SQL> select code,sum(decode(tr,-1,request_qty,0)) qiqian,sum(decode(tr,0,request_qty,0)) a0,sum(deco
    de(tr,1,request_qty,0)) a1,
      2  sum(decode(tr,2,request_qty,0)) a2,sum(decode(tr,3,request_qty,0)) a3,
      3  sum(decode(tr,4,request_qty,0)) a4
      4  from (
      5  select code,request_time,request_qty,decode(sign(tr),-1,-1,tr) tr from (
      6  select code,request_time,request_qty,trunc((
      7  to_date(request_time,'mm/dd')-(
      8  sysdate-decode(to_char(sysdate,'d'),1,7,to_char(sysdate,'d')-1)+1))/7) tr from temp
      9  ) t) tt group by code;CODE           QIQIAN         A0         A1         A2         A3         A4
    ---------- ---------- ---------- ---------- ---------- ---------- ----------
    xxx                 1          4          3          4          0          0
    yyy                 1          2          6          4          0          0已用时间:  00: 00: 00.10
    SQL>
      

  4.   

    谢谢,
    我是说比如A3,A4就不要出来,因为表里面的数据根本就没有那些周
    我就是说有多少周就出多少周,但不超过20周。
    像这次的例子就只有3周的(不包括期前),不要后面的A4,A5,怎么作
      

  5.   

    有多少周就出多少周?
    写个块结构,还必须要用动态SQL来实现查询。
      

  6.   

    但是周是不固定的阿,怎么写循环
    能不能用pl/sql搞个例子给我看看