求一SQL语句

520NET 2010-10-15 10:58:47
表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
...全文
83 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
心中的彩虹 2010-10-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wkc168 的回复:]
引用楼主 520net 的回复:
表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、获取……
[/Quote]



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







minitoy 2010-10-15
  • 打赏
  • 举报
回复
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'
520NET 2010-10-15
  • 打赏
  • 举报
回复
我改了一下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');
Diza1986 2010-10-15
  • 打赏
  • 举报
回复
是这需求不?

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

520NET 2010-10-15
  • 打赏
  • 举报
回复
不对,我改了一下,查询出来的还有其它日期的数据,再次求解,谢谢!
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)
minitoy 2010-10-15
  • 打赏
  • 举报
回复
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.时间)
呵呵,忘记了时间限定.
心中的彩虹 2010-10-15
  • 打赏
  • 举报
回复
[Quote=引用楼主 520net 的回复:]
表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、……
[/Quote]



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


minitoy 2010-10-15
  • 打赏
  • 举报
回复
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.到达)

3,492

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧