我想出了这个SQL语句:
SELECT *
FROM ( SELECT FID,
YEAR,
STATE,
CONNECT_BY_ISLEAF IS_LEAF
FROM (SELECT year YEAR,
state STATE,
id ID,
foreign_id FID
FROM yearsTable)
CONNECT BY ( PRIOR YEAR - 1 = YEAR
AND STATE = 'state2'
AND PRIOR FID = FID
AND LEVEL <= 2)
START WITH ( YEAR >= 2012 - 2
AND YEAR <= 2012 - 1
AND STATE = 'state1'))
CONNECT BY (PRIOR YEAR - 1 = YEAR AND PRIOR FID = FID)
START WITH (IS_LEAF = 0)
ORDER BY FID, YEAR ASC;
基本上搜索从上一年到下一年经历状态变化的所有年份。在本地测试机上工作正常,但在生产系统上中断。
我改变{{1在YEAR之后asc
。
到目前为止,我刚刚使用Oracle SQL Developer对此进行了测试,数据库版本为10g。
有人可以解释为什么输出会根据排序方向而改变吗?