Creating a PostgreSQL `tsrange` from two timestamps

怎甘沉沦 提交于 2019-12-05 02:50:49

问题


I am trying to create a tsrange (last Thursday to the previous Thursday) in a postgresql query but I get cast errors.

This is what I have got so far (starting off from this SO question).

WITH past_week AS (
    SELECT date_trunc('day', NOW() + (s::TEXT || ' day')::INTERVAL)::TIMESTAMP(0) AS day 
    FROM generate_series(-7, 0, 1) AS s)
SELECT (
date_trunc('day', (SELECT day FROM past_week WHERE EXTRACT(DOW FROM day) = '4') - '7 day'::INTERVAL),
date_trunc('day', (SELECT day FROM past_week WHERE EXTRACT(DOW FROM day) = '4')));

And this is the result (correct value, but not format, since it's not a range):

                      row                      
-----------------------------------------------
 ("2015-10-29 00:00:00","2015-11-05 00:00:00")
(1 row)

Now, there are 2 main things that bug me:

  1. If I try and add a ::tsrange right before the end of the query, the interpreter complains that:

    ERROR: cannot cast type record to tsrange LINE 6: ...ROM past_week WHERE EXTRACT(DOW FROM day) = '4')))::tsrange;

  2. I would love to avoid repetition, but I'm not that proficient in SQL to know how. Any improvement is more than welcome.


回答1:


Use tsrange() constructor:

WITH past_week AS (
    SELECT date_trunc('day', NOW() + (s::TEXT || ' day')::INTERVAL)::TIMESTAMP(0) AS day 
    FROM generate_series(-7, 0, 1) AS s)
SELECT tsrange(
    date_trunc('day', 
        (SELECT day FROM past_week 
        WHERE EXTRACT(DOW FROM day) = '4') - '7 day'::INTERVAL),
    date_trunc('day', 
        (SELECT day FROM past_week 
        WHERE EXTRACT(DOW FROM day) = '4')));

                    tsrange                    
-----------------------------------------------
 ["2015-10-29 00:00:00","2015-11-05 00:00:00")
(1 row)

Using CURRENT_DATE your query may be as simple as:

WITH previous_thursday AS (
    SELECT CURRENT_DATE- EXTRACT(DOW FROM CURRENT_DATE)::int+ 4 AS thursday
    )
SELECT tsrange(thursday- '7d'::INTERVAL, thursday)
FROM previous_thursday;


来源:https://stackoverflow.com/questions/33566087/creating-a-postgresql-tsrange-from-two-timestamps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!