Round date to 10 minutes interval

后端 未结 7 2051
独厮守ぢ
独厮守ぢ 2021-02-19 07:26

I have a DATE column that I want to round to the next-lower 10 minute interval in a query (see example below).

I managed to do it by truncating the seconds

7条回答
  •  半阙折子戏
    2021-02-19 08:11

    Not necessarily any better, but another method:

    WITH test_data AS (
            SELECT TO_DATE('2010-01-01 10:00:00', 'YYYY-MM-DD HH24:MI:SS') d FROM dual
      UNION SELECT TO_DATE('2010-01-01 10:05:00', 'YYYY-MM-DD HH24:MI:SS') d FROM dual
      UNION SELECT TO_DATE('2010-01-01 10:09:59', 'YYYY-MM-DD HH24:MI:SS') d FROM dual
      UNION SELECT TO_DATE('2010-01-01 10:10:00', 'YYYY-MM-DD HH24:MI:SS') d FROM dual
      UNION SELECT TO_DATE('2099-01-01 10:00:33', 'YYYY-MM-DD HH24:MI:SS') d FROM dual
    )
    -- #end of test-data
    SELECT
      d, TRUNC(d) + FLOOR((d-TRUNC(d))*24*6)/(24*6)
    FROM test_data
    

提交回复
热议问题