Sql 使用';当';select语句中的时间戳为空

Sql 使用';当';select语句中的时间戳为空,sql,postgresql,case,case-when,Sql,Postgresql,Case,Case When,我在postgresql中使用了一个案例,当块被阻塞时,我将确定结果将如何显示。该区块如下所示: (case when four.status = 'active' and (four.expiration > (current_date + integer '{{expire_window}}')) then 'Active' when four.status = 'active' and (four.expiration is null) then 'Active'

我在postgresql中使用了一个
案例,当
块被阻塞时,我将确定结果将如何显示。该区块如下所示:

(case 
    when four.status = 'active' and (four.expiration > (current_date + integer '{{expire_window}}')) then 'Active'
    when four.status = 'active' and (four.expiration is null) then 'Active'
    when four.status = 'pending' then 'Pending'
    when four.status = 'active' and (four.expiration <= (current_date + integer '{{expire_window}}')) then 'Expiring'

        else 'Missing' end) as certificate
(案例)
当four.status='active'和(four.expiration>(当前日期+整数'{{expire\u window}}')时,则为'active'
当four.status='active'和(four.expiration为null)时,则为'active'
当four.status='pending'然后是'pending'

当four.status='active'和(four.expiration检查这是否适用于您

(case 
when (four.status = 'active' 
        and ((four.expiration is null)                   
             or (four.expiration > (current_date + integer '{{expire_window}}'))
            )) then 'Active'
when four.status = 'pending' then 'Pending'
when four.status = 'active' and (four.expiration <= (current_date + integer '{{expire_window}}')) then 'Expiring'

    else 'Missing' end) as certificate
(案例)
当(4.status='active'
和((4.expiration为空)
或者(four.expiration>(当前日期+整数“{{expire\u window}}”)
))然后“主动”
当four.status='pending'然后是'pending'
当four.status='active'和(four.expiration时,我无法复制它

with four(status, expiration) as ( values
    ('active'::text, '2014-12-31'::timestamp),
    ('active', null),
    ('pending', null),
    ('active', '2014-08-15')
)
select
case
    when status = 'active' and (expiration > (current_date + 30)) then 'Active'
    when status = 'active' and (expiration is null) then 'Active'
    when status = 'pending' then 'Pending'
    when status = 'active' and (expiration <= (current_date + 30)) then 'Expiring'
    else 'Missing'
end as certificate
from four
;
 certificate 
-------------
 Active
 Active
 Pending
 Expiring

它看起来是正确的,所以我要说检查你的假设。你期望用这个条件捕获的实际行是否与你指定的条件匹配?我不知道你在哪里拼错了,但是你在你的问题中同时使用了
four.status='active'
four.status='active'
,所以请注意:等式运算符或者
=
区分大小写。如果您在表中同时使用
'active'
'active'
,则可能需要测试值,如
lower(four.status)='active'
,或者使用citext(),或者只修复数据(例如,使用触发器)。
case status
    when 'active' then
        case
            when
                expiration is null or
                expiration > current_date + 30
                then 'Active'
            else 'Expiring'
        end
    when 'pending' then 'Pending'
    else 'Missing'
end as certificate