我有以下表格sequence number
和Station codes
。我想带两个站之间的站点列表。
如果我这样做
select Station Code
from Table1
where Station Code BETWEEN 'BWI' and 'PHL'
它应该根据这样的顺序给我站 - > (BWI,BAL,WIL,PHL)。
它给我以下的电台 - > (BWI,PHL,NWK,NYP)。
请让我知道我是谁带来了所需的价值?我在下面的表1中添加了示例数据。
表1
Sequence Number Station Code
1 WAS
2 BWI
3 BAL
4 WIL
5 PHL
6 NWK
7 NYP
答案 0 :(得分:1)
如果您不知道电台的排序,可以使用:
select t1.Code
from Table1 t1 cross join
(select min(number) as minn, max(number) maxn
from table1
where code in ('BWI' and 'PHL')
tt
where t1.number between minn and maxnn;
答案 1 :(得分:0)
问题是
'BWI' and 'PHL'
返回所有代码位于字符串比较之间的行。
你想要
SELECT *
FROM Table1
WHERE Sequence Number
between (SELECT Sequence Number FROM Table1 WHERE Station Code ='BAL')
and (SELECT Sequence Number FROM Table1 WHERE Station Code ='PHL')
答案 2 :(得分:0)
由于它是用Oracle标记的,因此这是一个不需要多次表扫描的Oracle解决方案:
Oracle 11g R2架构设置:
CREATE TABLE table1 ( Sequence_Number, Station_Code ) AS
SELECT 1, 'WAS' FROM DUAL
UNION ALL SELECT 2, 'BWI' FROM DUAL
UNION ALL SELECT 3, 'BAL' FROM DUAL
UNION ALL SELECT 4, 'WIL' FROM DUAL
UNION ALL SELECT 5, 'PHL' FROM DUAL
UNION ALL SELECT 6, 'NWK' FROM DUAL
UNION ALL SELECT 7, 'NYP' FROM DUAL
查询1 :
SELECT *
FROM table1
START WITH Station_Code = 'BWI'
CONNECT BY PRIOR Sequence_Number = Sequence_Number - 1
AND PRIOR Station_Code <> 'PHL'
<强> Results 强>:
| SEQUENCE_NUMBER | STATION_CODE |
|-----------------|--------------|
| 2 | BWI |
| 3 | BAL |
| 4 | WIL |
| 5 | PHL |
答案 3 :(得分:0)
从table1中选择*
之间的顺序
(从表中选择sequence_number,其中站号=&#39; BWI&#39;)
和
(从表格中选择sequence_number,其中电台代码=&#39; PHL&#39;);