你也可以使用一個resursive CTE爲避免創建一個輔助表:
WITH "CTE"
AS
(
SELECT "id", "starttime" FROM "data"
UNION ALL
SELECT "id", DATEADD(MINUTE, 15, "starttime")
FROM "CTE"
WHERE "starttime" < (SELECT "endtime" FROM "data" AS D2 WHERE D2."id" = CTE."id")
)
SELECT * FROM "CTE"
ORDER BY "ID"
見SQL-Fiddle-Demo!
上面的查詢還假定在數據表開始和結束時間的組合差異整除15
我已編輯的查詢與日期時間不屬於由15整除的工作 - 如果你選擇的時間間隔15分鐘不適合在最後計算的區間,將被拒絕:
WITH "CTE"
AS
(
SELECT "id", "starttime" FROM "data"
UNION ALL
SELECT "id", DATEADD(MINUTE, 15, "starttime")
FROM "CTE"
WHERE "starttime" <= (SELECT DATEADD(MINUTE, -15, "endtime") FROM "data" AS D2 WHERE D2."id" = CTE."id")
)
SELECT * FROM "CTE"
ORDER BY "ID"
如果你想在你的結果拒絕(在撥弄-演示id 2, timestamp "12:00:00"
)最後的時間戳只是改變<=
到<
。如果您正在以半開放間隔工作,這也是相關的。
見edited SQL-Fiddle-Demo
您可以加入預期的一些數據結果呢? – WKordos