案件情况未得到预期的结果

时间:2019-07-17 13:46:21

标签: postgresql

在我的查询中,我有一些情况要在lastoccurrence列中查看小时数,然后如在特定情况下所示,它应该在aging_range列中6 hrs, 12 hrs, 24 hrs, 48 hrs or else > 1 week中打印。问题是,即使我在图片hours = 1中收到> 1 week。我的查询出了什么问题?

查询:

CREATE OR REPLACE VIEW schemaA.test
AS SELECT tableA.lastoccurrence,
    date_part('hours'::text, timezone('GMT'::text, now()) - tableA.lastoccurrence) AS hours,
    date_part('days'::text, timezone('GMT'::text, now()) - tableA.lastoccurrence) AS days,
    age(timezone('GMT'::text, now()), tableA.lastoccurrence) AS age,
        CASE
            WHEN date_part('hours'::text, timezone('GMT'::text, now()) - tableA.lastoccurrence) >= 1::double precision AND date_part('hours'::text, timezone('GMT'::text, now()) - tableA.lastoccurrence) <= 6::double precision AND date_part('days'::text, timezone('GMT'::text, now()) - tableA.lastoccurrence) = 0::double precision THEN '6 hrs'::text
            WHEN date_part('hours'::text, timezone('GMT'::text, now()) - tableA.lastoccurrence) >= 7::double precision AND date_part('hours'::text, timezone('GMT'::text, now()) - tableA.lastoccurrence) <= 12::double precision AND date_part('days'::text, timezone('GMT'::text, now()) - tableA.lastoccurrence) = 0::double precision THEN '12 hrs'::text
            WHEN date_part('hours'::text, timezone('GMT'::text, now()) - tableA.lastoccurrence) >= 13::double precision AND date_part('hours'::text, timezone('GMT'::text, now()) - tableA.lastoccurrence) <= 24::double precision AND date_part('days'::text, timezone('GMT'::text, now()) - tableA.lastoccurrence) = 0::double precision THEN '24 hrs'::text
            WHEN date_part('hours'::text, timezone('GMT'::text, now()) - tableA.lastoccurrence) >= 25::double precision AND date_part('hours'::text, timezone('GMT'::text, now()) - tableA.lastoccurrence) <= 48::double precision AND date_part('days'::text, timezone('GMT'::text, now()) - tableA.lastoccurrence) = 0::double precision THEN '48 hrs'::text
            ELSE '> 1 week'::text
        END AS aging_range
         FROM tmp.tableA;

结果: enter image description here

1 个答案:

答案 0 :(得分:0)

问题是运行查询时,时间戳记少于过去一个小时,并且您的CASE语句不包含“ hours = 0”的大小写,因此{{1} }分支。

ELSE表达式过于复杂。我建议类似的东西:

CASE