表TEST结构和内容如下:
日期 时间    编号 始发 到达
12   09:00   013  ZA   TA
12   10:00   013  TA   XA
12   09:30   020  ZA   YA
12   10:10   027  ZA   UA
12   11:20   034  SA   RA
12   13:10   042  XA   UA
12   14:20   042  ZA   RA要求:
1、获取日期为12号的且始发的是ZA
2、如始发的不是ZA且与“其它”始发是ZA的编号相同,且到达站是“其它”始发站,时间也在“其它”后面
结果如下:
12   09:00   013  ZA   TA
12   10:00   013  TA   XA
12   09:30   020  ZA   YA
12   10:10   027  ZA   UA
12   14:20   042  ZA   RA

解决方案 »

  1.   

    select * from test 
     where 日期='12' and 始发='ZA'
    union all
    select * from test a 
     where exists(select 1 from test b where b.日期='12' and b.始发='ZA' and a.编号=b.编号 and a.始发!=b.始发 and a.到达!=b.到达)
      

  2.   


    select * from test a where not exists(select 1 from test b where a.编号=b.编号 and a.时间>b.时间) and a.始发='ZA' and a.日期='12'
      

  3.   

    select * from test 
     where 日期='12' and 始发='ZA'
    union all
    select * from test a 
     where exists(select 1 from test b where b.日期='12' and b.始发='ZA' and a.编号=b.编号 and a.始发!=b.始发 and a.到达!=b.到达 and a.时间>b.时间)呵呵,忘记了时间限定.
      

  4.   

    不对,我改了一下,查询出来的还有其它日期的数据,再次求解,谢谢!
    select c.*
      from t2001 c
     where c.flight_date = to_date('2010-10-15', 'yyyy-MM-dd')
       and c.departure_airport = 'ZGSZ'
    union all
    select *
      from t2001 a
     where exists (select 1
              from t2001 b
             where b.flight_date = to_date('2010-10-15', 'yyyy-MM-dd')
               and b.departure_airport = 'ZGSZ'
               and a.flight_no = b.flight_no
               and a.departure_airport != b.departure_airport
               and a.arrival_airport != b.arrival_airport
               and a.std > b.std)
      

  5.   

    我改了一下SQL,这个根本查不出来数据,谢谢!
    select *
      from t2001 a
     where not exists (select 1
              from t2001 b
             where a.flight_no = b.flight_no
               and a.std > b.std)
       and a.departure_airport = 'ZGSZ'
       and a.flight_date = to_date('2010-10-15', 'yyyy-MM-dd');
      

  6.   

    是这需求不?select * from TEST
    where 日期 = '12' --就算字符型
    start with 始发 = 'ZA'
    connect by 到达 = prior 始发 and 编号 = prior 编号 and 时间 > prior 时间 
      

  7.   

    select * from test 
     where 日期='12' and 始发='ZA'
    union all
    select * from test a 
     where exists(select 1 from test b where b.日期='12' and b.始发='ZA' and a.编号=b.编号 and a.始发!=b.始发 and a.到达!=b.到达 and a.时间>b.时间)
       and a.日期='12'
      

  8.   


    SQL> with tb as
      2  (
      3  select '12' rq,'09:00' sj,'013' bh,'ZA' st,'TA' en from dual union all
      4  select '12','10:00','013','TA','XA' from dual union all
      5  select '12','09:30','020','ZA','YA' from dual union all
      6  select '12','10:10','027','ZA','UA' from dual union all
      7  select '12','11:20','034','SA','RA' from dual union all
      8  select '12','13:10','042','XA','UA' from dual union all
      9  select '12','14:20','042','ZA','RA' from dual 
     10  ) 
     11  select * from tb where rq='12' and st='ZA'
     12  union 
     13  select * from tb a 
     14  where exists(select 1 from tb b where a.bh=b.bh and a.sj>b.sj and b.rq='12' and b.st='ZA')
     15  /RQ SJ    BH  ST EN
    -- ----- --- -- --
    12 09:00 013 ZA TA
    12 09:30 020 ZA YA
    12 10:00 013 TA XA
    12 10:10 027 ZA UA
    12 14:20 042 ZA RA