0

我可能会在整个周末解决这个问题,周五晚些时候很难集中注意力......

下面的查询 1 返回行程,在给定位置具有到达和离开值。

下面的查询 2 返回一个行程,在第二个位置具有到达和离开值。

这两个查询很简单。

我只想返回查询 2的结果,当查询 2 的出发时间恰好在查询1 的到达和离开时间之间。

从本质上讲,这将给我在已经在“BER3”的旅行出发之前离开“BER2”的旅行的任何实例。

查询 1

SELECT     RTDEV.TRIP_HEADERS.TRIP_NAME, RTDEV.TRIP_BODIES.ARRIVAL_TIME, RTDEV.TRIP_BODIES.DEPARTURE_TIME
FROM         RTDEV.TRIP_HEADERS INNER JOIN
                      RTDEV.TRIP_BODIES ON RTDEV.TRIP_HEADERS.TME_ID = RTDEV.TRIP_BODIES.TME_ID AND 
                      RTDEV.TRIP_HEADERS.THR_ID = RTDEV.TRIP_BODIES.THR_ID INNER JOIN
                      RTDEV.TNODED ON RTDEV.TNODED.NID = RTDEV.TRIP_BODIES.CURRENT_NODE
WHERE     (RTDEV.TNODED.STRSHORTNAME = 'BER3') AND (RTDEV.TRIP_BODIES.TME_ID = 9860)

查询 2

SELECT     TRIP_HEADERS_1.TRIP_NAME, TRIP_BODIES_1.ARRIVAL_TIME, TRIP_BODIES_1.DEPARTURE_TIME
FROM         RTDEV.TRIP_HEADERS TRIP_HEADERS_1 INNER JOIN
                      RTDEV.TRIP_BODIES TRIP_BODIES_1 ON TRIP_HEADERS_1.TME_ID = TRIP_BODIES_1.TME_ID AND 
                      TRIP_HEADERS_1.THR_ID = TRIP_BODIES_1.THR_ID INNER JOIN
                      RTDEV.TNODED TNODED_1 ON TNODED_1.NID = TRIP_BODIES_1.CURRENT_NODE
WHERE     (TNODED_1.STRSHORTNAME = 'BER2') AND (TRIP_BODIES_1.TME_ID = 9860)

有什么建议么?

4

1 回答 1

0

这很简单 - 您可以像这样将query2内联视图(FROM子句中的视图)放入内部:

SELECT     TRIP_HEADERS_1.TRIP_NAME, TRIP_BODIES_1.ARRIVAL_TIME, TRIP_BODIES_1.DEPARTURE_TIME
FROM         RTDEV.TRIP_HEADERS TRIP_HEADERS_1 INNER JOIN
                      RTDEV.TRIP_BODIES TRIP_BODIES_1 ON TRIP_HEADERS_1.TME_ID = TRIP_BODIES_1.TME_ID AND 
                      TRIP_HEADERS_1.THR_ID = TRIP_BODIES_1.THR_ID INNER JOIN
                      RTDEV.TNODED TNODED_1 ON TNODED_1.NID = TRIP_BODIES_1.CURRENT_NODE
-- PUT YOUR QUERY 2 HERE --
JOIN (SELECT     RTDEV.TRIP_HEADERS.TRIP_NAME, RTDEV.TRIP_BODIES.ARRIVAL_TIME, RTDEV.TRIP_BODIES.DEPARTURE_TIME
        FROM         RTDEV.TRIP_HEADERS INNER JOIN
                              RTDEV.TRIP_BODIES ON RTDEV.TRIP_HEADERS.TME_ID = RTDEV.TRIP_BODIES.TME_ID AND 
                              RTDEV.TRIP_HEADERS.THR_ID = RTDEV.TRIP_BODIES.THR_ID INNER JOIN
                              RTDEV.TNODED ON RTDEV.TNODED.NID = RTDEV.TRIP_BODIES.CURRENT_NODE
        WHERE     (RTDEV.TNODED.STRSHORTNAME = 'BER3') AND (RTDEV.TRIP_BODIES.TME_ID = 9860)) query2
-- AND PUT A JOIN CONDITION --
        ON TRIP_BODIES_1.DEPARTURE_TIME BETWEEN query2.ARRIVAL_TIME AND query2.DEPARTURE_TIME
WHERE     (TNODED_1.STRSHORTNAME = 'BER2') AND (TRIP_BODIES_1.TME_ID = 9860)
于 2013-08-16T05:12:13.347 回答