WITH A AS
(SELECT 1 AS CONCAT_GROUP,TO_CHAR(SYSDATE,'YYYY-MM-DD')||' 06:00:00' AS START_TIME,TO_CHAR(SYSDATE,'YYYY-MM-DD')||' 08:00:00' AS END_TIME FROM DUAL
UNION
SELECT 1,TO_CHAR(SYSDATE,'YYYY-MM-DD')||' 10:00:00',TO_CHAR(SYSDATE,'YYYY-MM-DD')||' 11:00:00' FROM DUAL
UNION
SELECT 1,TO_CHAR(SYSDATE,'YYYY-MM-DD')||' 10:30:00',TO_CHAR(SYSDATE,'YYYY-MM-DD')||' 12:00:00' FROM DUAL
UNION
SELECT 1,TO_CHAR(SYSDATE,'YYYY-MM-DD')||' 15:30:00',TO_CHAR(SYSDATE,'YYYY-MM-DD')||' 16:00:00' FROM DUAL)
SELECT
C.CONCAT_GROUP,
C.START_TIME,
C.END_TIME
FROM
(
SELECT
B.*,
ROW_NUMBER()OVER(PARTITION BY B.CONCAT_GROUP, B.START_TIME ORDER BY END_TIME DESC) AS RANK
FROM
(
SELECT
A.CONCAT_GROUP,
CASE WHEN LAG(A.END_TIME)OVER(PARTITION BY A.CONCAT_GROUP ORDER BY A.END_TIME) BETWEEN A.START_TIME AND A.END_TIME
THEN LAG(A.START_TIME)OVER(PARTITION BY A.CONCAT_GROUP ORDER BY A.START_TIME)
ELSE A.START_TIME
END AS START_TIME,
A.END_TIME
FROM A
) B
)C
WHERE
C.RANK=1
以上代碼僅限合併2條連續時間段資料。