You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何按状态流转顺序更新日期列?SQL行转列缺省值调整需求

修改后的查询语句

select 
NUMBER_ID,
deadline_date,
(array_agg(STATUS) within group(order by updated_at_datetime desc)[0])::varchar as last_status,
-- 调整clicked_date的优先级取值逻辑
coalesce(
    max(case when STATUS = 'clicked' THEN updated_at_datetime END),
    max(case when STATUS = 'claimed' THEN updated_at_datetime END),
    max(case when STATUS = 'bought' THEN updated_at_datetime END),
    '2999-12-31'::datetime
) as clicked_date,
coalesce(max(case when STATUS = 'claimed' THEN updated_at_datetime END),'2999-12-31'::datetime) as claimed_date,
coalesce(max(case when STATUS = 'bought' THEN updated_at_datetime END),'2999-12-31'::datetime) as bought_date
from TBL_A a
group by 1,2
order by number_id

调整说明

仅修改了clicked_date字段的coalesce参数顺序,按需求实现优先级取值:

  1. 优先取当前number_id下CLICKED状态的更新时间
  2. 不存在CLICKED记录则取同ID下CLAIMED状态的更新时间
  3. 也不存在CLAIMED记录则取同ID下BOUGHT状态的更新时间
  4. 最终兜底值保留2999-12-31(实际不会触发,因为参与分组的ID至少存在一条状态记录)
    claimed_date和bought_date的逻辑完全保留原有规则,不存在时仍默认赋值为2999-12-31。

内容的提问来源于stack exchange,提问作者user3461502

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 02:27:00