我正在尝试在 postgresql 查询中创建一个 tsrange(上周四到上周四),但我遇到了转换错误。

这是我到目前为止所得到的(从 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')));

这是结果(正确的值,但不是格式,因为它不是一个范围):
                      row
-----------------------------------------------
 ("2015-10-29 00:00:00","2015-11-05 00:00:00")
(1 row)

现在,有两个主要问题困扰着我:
  • 如果我尝试在查询结束之前添加 ::tsrange,解释器会提示:

    错误:无法将类型记录转换为 tsrange
    第 6 行:...ROM 过去一周 WHERE EXTRACT(DOW FROM day) = '4')))::tsrange;
  • 我很想避免重复,但我不太精通 SQL,不知道该怎么做。任何改进都非常受欢迎。
  • 最佳答案

    使用 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)
    

    使用 CURRENT_DATE 您的查询可能很简单:
    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;
    

    10-08 19:58