我正在尝试检查自从首次致电客户以来的时间。我正在使用case语句对照当前系统时间对此进行检查。
select sale_id,case when ((CURRENT_TIMESTAMP - min(call_date) >= '500 days' THEN 'more than 500 days'
when ((CURRENT_TIMESTAMP - min(call_date) >= '300 days' THEN 'more than 300 days'
else 'Less than 300 days' end as Aging
from sales
我一直在执行查询失败。无效的操作:“ THEN”或整齐的语法错误。
我正在使用Amazon Redshift DB。 谁能帮忙。
答案 0 :(得分:0)
除了括号外,您还使用了min()
,而没有group by
。我还怀疑sale_id
代表客户。
所以,我怀疑您想要类似的东西:
select customer_id, -- or whatever the right column is
(case when CURRENT_TIMESTAMP - min(call_date) >= '500 days' then 'more than 500 days'
when CURRENT_TIMESTAMP - min(call_date) >= '300 days' then 'more than 300 days'
else 'Less than 300 days'
end) as Aging
from sales
group by customer_id;