在SQL中,我有一个如下所示的表:

enter image description here

我想根据‘Seq_Numbers’组合来自每个‘id_Parent_Record’的‘Value_New’的值,这意味着它应该始终从1开始.

作为一个例子,637454609993200总共有4个移动用于‘现场_已更改’=‘任务_工作流’.

预期输出应完全如下所示:

初始推荐->IRU->已批准调整->结算

这是我目前所在的代码:

select ac."id_Parent_Record"
,ac."Field_Changed"
,ac."Value_Previous"
,ac."Value_New"
,ac."Changed_Date" 
,row_number() over (partition by "id_Parent_Record", "Field_Changed"  order by "Changed_Date" asc) as "Seq_Number"
from public.api_changelog ac
where ac."Field_Changed" IN ('_id_Matter','Task_Workflow')
and ac."id_Parent_Record" = '637454609993200';

预期的输出应该有一个名为‘过渡’的新字段,对于帐户637454609993200,其每行的值应该如下所示,该字段_CHANGE等于任务_工作流:

初始推荐->IRU->已批准调整->结算

推荐答案

STRING_AGG函数应该能够实现这一点.

每个Value_New应由' -> '分隔,并应按Changed_Date字段排序.AGROUP BY将需要包括在内.

select ac."id_Parent_Record"
,ac."Field_Changed"
, STRING_AGG(ac."Value_New", ' -> ' ORDER BY ac."Changed_Date" asc) AS "Transitions"
from public.api_changelog ac
where ac."Field_Changed" IN ('_id_Matter','Task_Workflow')
and ac."id_Parent_Record" = '637454609993200'
GROUP BY ac."id_Parent_Record"
,ac."Field_Changed"
;

输出:

id_Parent_Record Field_Changed Transitions
637454609993200 _id_Matter 637417579791350
637454609993200 Task_Workflow Initial Referral -> IRU -> Adjustment Approved -> Settlement

可以使用CTE将表重新联接回原始结果集

WITH transitions AS (
    select ac."id_Parent_Record"
    ,ac."Field_Changed"
    , STRING_AGG(ac."Value_New", ' -> ' ORDER BY ac."Changed_Date" asc) AS "Transitions"
    from public.api_changelog ac
    where ac."Field_Changed" IN ('_id_Matter','Task_Workflow')
    and ac."id_Parent_Record" = '637454609993200'
    GROUP BY ac."id_Parent_Record"
    ,ac."Field_Changed"
)

select ac."id_Parent_Record"
,ac."Field_Changed"
,ac."Value_Previous"
,ac."Value_New"
,ac."Changed_Date" 
, t."Transitions"
from public.api_changelog ac
INNER JOIN transitions t
    ON ac."id_Parent_Record" = t."id_Parent_Record"
    AND ac."Field_Changed" = t."Field_Changed"
;
id_Parent_Record Field_Changed Value_Previous Value_New Changed_Date Transitions
637454609993200 _id_Matter 637417579791350 2021-01-07 637417579791350
637454609993200 Task_Workflow Initial Referral 2021-01-05 Initial Referral -> IRU -> Adjustment Approved -> Settlement
637454609993200 Task_Workflow Initial Referral IRU 2021-01-06 Initial Referral -> IRU -> Adjustment Approved -> Settlement
637454609993200 Task_Workflow IRU Adjustment Approved 2021-03-26 Initial Referral -> IRU -> Adjustment Approved -> Settlement
637454609993200 Task_Workflow Adjustment Approved Settlement 2021-06-02 Initial Referral -> IRU -> Adjustment Approved -> Settlement

Sql相关问答推荐

PostgreSQL:获取每家店铺收入最高的员工

SQL(PostgreSQL)从条件创建点表

SQL -滞后于上一个非重复值

SQL:如何在表中同时使用GROUPING和CONDITION?

如何将`now()`作为SQL插入语句的一部分?

如何实现同一列的递归计算?

我需要一个regexp_like来只验证字母D或T、数字和管道

仅在日期相隔时递增(Oracle SQL)

通过对象分离实现原子性

在 R 值的 SQL 块中显示值

如何使用最后一个非 NULL 值在 PostgreSQL 列中填充 NULL 值

SQL 中的第一个值和倒数第二个值

SQL Server中使用min()和max()从选定的特定值id表中删除不必要的时间

如何使用SELECT语句进行左连接,并根据右表中的特定值过滤结果?

在Snowflake中,如何将以逗号和连字符分隔的多个混合数值拆分成数字列表

HIVE SQL where 子句未按预期工作

我如何编写一个遍历数组数组并将所有值连接成一个字符串的 postgres 函数

如何在 Oracle 中获取此变量的值?

使用 SQL 表中的连接列删除重复记录

为什么这是 AND,OR with NULL 的真值表?