一张表tableAid  field1 field2   price    date1         date2
1    3      4       200      2007.01.01    2007.01.31
2    3      4       180      2007.02.01    2007.02.28
3    3      4       270      2007.03.01    2007.03.15
4    2      2       250      2007.01.01    2007.02.28
5    1      2       300      2007.01.01    2007.01.30
...我要查出范围在 2007.01.29 - 2007.02.02 之间的每天的price的所有结果, 相同的field1和field2才是同一个项目的,结果应该是:field1 field2   price  price  price  price  price
3        4       200    200    200    180    180
2        2       250    250    250    250    250
如何写这个查询语句?
可能写一个语句有点复杂,不能完成,是否必须存储过程才能完成?

解决方案 »

  1.   

    select ....汗,怎么可能同时有这么多个price
      

  2.   

    以时间分组!汗,怎么可能同时有这么多个price?
      

  3.   

    --测试数据
    create table tablea
    (id int, field1 int,field2 int,price int,date1 date,date2 date)
    id  field1 field2   price    date1         date2
    insert into tablea
    select 1,3,4,200,to_date('2007.01.01','yyyy-mm-dd'),to_date('2007.01.31','yyyy-mm-dd') from dual union all
    select 2,3,4,180,to_date('2007.02.01','yyyy-mm-dd'),to_date('2007.02.28','yyyy-mm-dd') from dual union all
    select 3,3,4,270,to_date('2007.03.01','yyyy-mm-dd'),to_date('2007.03.15','yyyy-mm-dd') from dual union all
    select 4,2,2,250,to_date('2007.01.01','yyyy-mm-dd'),to_date('2007.02.28','yyyy-mm-dd') from dual union all
    select 5,1,2,300,to_date('2007.01.01','yyyy-mm-dd'),to_date('2007.01.30','yyyy-mm-dd') from dual;
    --存储过程
    create or replace procedure (sdate in varchar2(20),eDate in varchar2(20),rst out sys_refcursor )
    is
      days int;
      IntI int;
      fdate varchar2(20);
      sqlStr varchar2(4000);
    begin
      sqlStr:='select t.field1,t.field2,';
      select to_date(sdate,'yyyy-mm-dd') - to_date(eDate,'yyyy.mm.dd') into days from dual;
      for IntI in 0..days loop
        fdate:=to_char(to_date(sdate,'yyyy-mm-dd')+IntI,'yyyy-mm-dd');
        sqlStr:=sqlStr||'(select price from tablea where field1=t.field1 and field2=t.field2 and (to_date('''||fdate||''',''yyyy-mm-dd'')>=date1 and to_date('''||fdate||''',''yyyy-mm-dd'')<=date2 )) price'||IntI||', ';   
      end loop;
      sqlStr:=substr( sqlstr,1,length( sqlstr )-2);
      sqlStr:=sqlStr||' from (select field1,field2, min(date1) date1,max(date2) date2  from tablea group by field1,field2) t  where date1<=to_date('||sdate||',''yyyy.mm.dd'') and date2>=to_date('||edate||',''yyyy-mm-dd'') ';
      insert into ttt values( sqlstr );
      open rst for sqlstr;
     
    end;--测试结果
    2 2 250 250 250 250 250
    3 4 200 200 200 180 180
      

  4.   

    感谢帮助,但是:
    sqlStr:=substr( sqlstr,1,length( sqlstr )-2); 应该是
    sqlStr:=substr( sqlstr,1,length( sqlstr )-1); 吧?
    而且输出里没有价格
      

  5.   

    相同的field1和field2才是同一个项目的,结果应该是:field1 field2   price  price  price  price  price
    3        4       200    200    200    180    180
    2        2       250    250    250    250    250
    可是第一行的field1与field2并不相同呀?
      

  6.   

    你看错意思了,我是说,field1和field1相同,field2和field2相同,同时相同