选择特定组的最旧记录,直到它更改模式,在SQL中。

huangapple go评论57阅读模式
英文:

Select the oldest record of a certain group until it changes pattern, in SQL

问题

我正在尝试获取以下表中每个状态更新/更改的最旧记录。

表(status_updates):

id entity_id status date
7 2 Approved 2022-02-10
6 2 Approved 2022-02-05
5 2 Approved 2022-02-04
4 2 OnHold 2022-02-04
3 2 OnHold 2022-02-03
2 2 Approved 2022-02-02
1 2 Approved 2022-02-01

需要的结果:

id entity_id status date
5 2 Approved 2022-02-04
3 2 OnHold 2022-02-03
1 2 Approved 2022-02-01

尝试过的:

select
    `status`,
    `created_at`
from
    `status_updates`
left join
    (select
        `id`,
        row_number() over (partition by status_updates.entity_id, status_updates.status order by status_updates.created_at asc) as sequence
    from
        `status_updates`)
    as `oldest_history`
    on
        `oldest_history`.`id` = `shipper_credit_histories`.`id`
where `sequence` = 1

已实现的结果:

id entity_id status date
3 2 OnHold 2022-02-03
1 2 Approved 2022-02-01
英文:

I am trying to get the oldest record for every status update/change in the following table.

Table (status_updates) :

id entity_id status date
7 2 Approved 2022-02-10
6 2 Approved 2022-02-05
5 2 Approved 2022-02-04
4 2 OnHold 2022-02-04
3 2 OnHold 2022-02-03
2 2 Approved 2022-02-02
1 2 Approved 2022-02-01

Result Needed :

id entity_id status date
5 2 Approved 2022-02-04
3 2 OnHold 2022-02-03
1 2 Approved 2022-02-01

Tried :

select
    `status`,
    `created_at`
from
    `status_updates`
left join
    (select
        `id`,
        row_number() over (partition by status_updates.entity_id, status_updates.status order by status_updates.created_at asc) as sequence
    from
        `status_updates`)
    as `oldest_history`
    on
        `oldest_history`.`id` = `shipper_credit_histories`.`id`
where `sequence` = 1

Result Achived :

id entity_id status date
3 2 OnHold 2022-02-03
1 2 Approved 2022-02-01

答案1

得分: 1

只使用滞后值:

select s.*
from (
    select id, status<>coalesce(lag(status) over (partition by entity_id order by id), '') status_change
    from status_updates
) ids
join status_updates s using (id)
where status_change
英文:

Just using lag:

select s.*
from (
    select id, status&lt;&gt;coalesce(lag(status) over (partition by entity_id order by id),&#39;&#39;) status_change
    from status_updates
) ids
join status_updates s using (id)
where status_change

答案2

得分: 0

以下是要翻译的内容:

这里是查询:

create table status_updates
(entity_id integer,
status varchar(32),
date date
);

insert into status_updates values (2, 'Approved', '2022-02-05');
insert into status_updates values (2, 'Approved', '2022-02-04');
insert into status_updates values (2, 'On Hold', '2022-02-04');
insert into status_updates values (2, 'On Hold', '2022-02-03');
insert into status_updates values (2, 'Approved', '2022-02-02');
insert into status_updates values (2, 'Approved', '2022-02-01');

select b.*
from status_updates a
right join status_updates b 
on a.status=b.status and a.date=(b.date - interval 1 day) 
where a.entity_id is null;

或者这个查询(如果您更喜欢左连接):

select a.*
from status_updates a
left join status_updates b
on a.status=b.status and a.date=(b.date + interval 1 day)
where b.entity_id is null;

在这两种情况下,您将看到预期的结果。

英文:

here are the queries:

create table status_updates
(entity_id integer,
status varchar(32),
date date
);

insert into status_updates values (2, &#39;Approved&#39;, &#39;2022-02-05&#39;);
insert into status_updates values (2, &#39;Approved&#39;, &#39;2022-02-04&#39;);
insert into status_updates values (2, &#39;On Hold&#39;, &#39;2022-02-04&#39;);
insert into status_updates values (2, &#39;On Hold&#39;, &#39;2022-02-03&#39;);
insert into status_updates values (2, &#39;Approved&#39;, &#39;2022-02-02&#39;);
insert into status_updates values (2, &#39;Approved&#39;, &#39;2022-02-01&#39;);

select b.*
from status_updates a
right join status_updates b 
on a.status=b.status and a.date=(b.date - interval 1 day) 
where a.entity_id is null;

or this query(if you prefer left join)

select a.*
from status_updates a
left join status_updates b
on a.status=b.status and a.date=(b.date + interval 1 day)
where b.entity_id is null;

in both you will see the expected result

答案3

得分: 0

第二种解决方案几乎相同,但是通过ID而不是日期进行连接。

create table status_updates
(id integer,
 entity_id integer,
 status varchar(32),
 date date
);

insert into status_updates values (7, 2, 'Approved', '2022-02-10');
insert into status_updates values (6, 2, 'Approved', '2022-02-05');
insert into status_updates values (5, 2, 'Approved', '2022-02-04');
insert into status_updates values (4, 2, 'On Hold', '2022-02-04');
insert into status_updates values (3, 2, 'On Hold', '2022-02-03');
insert into status_updates values (2, 2, 'Approved', '2022-02-02');
insert into status_updates values (1, 2, 'Approved', '2022-02-01');

select a.*
from status_updates a
         left join status_updates b
                   on a.status=b.status and a.id=b.id + 1
where b.entity_id is null;

结果与您期望的相同。

英文:

the second solution is almost the same, but join by id instead of date

create table status_updates
(id integer,
entity_id integer,
 status varchar(32),
 date date
);

insert into status_updates values (7, 2, &#39;Approved&#39;, &#39;2022-02-10&#39;);
insert into status_updates values (6, 2, &#39;Approved&#39;, &#39;2022-02-05&#39;);
insert into status_updates values (5, 2, &#39;Approved&#39;, &#39;2022-02-04&#39;);
insert into status_updates values (4, 2, &#39;On Hold&#39;, &#39;2022-02-04&#39;);
insert into status_updates values (3, 2, &#39;On Hold&#39;, &#39;2022-02-03&#39;);
insert into status_updates values (2, 2, &#39;Approved&#39;, &#39;2022-02-02&#39;);
insert into status_updates values (1, 2, &#39;Approved&#39;, &#39;2022-02-01&#39;);

select a.*
from status_updates a
         left join status_updates b
                   on a.status=b.status and a.id=b.id + 1
where b.entity_id is null;

result is the same what you expected

huangapple
  • 本文由 发表于 2023年2月14日 05:53:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定