Postgresql 使用触发器检索每个ID的最后记录
这是我的桌子:Postgresql 使用触发器检索每个ID的最后记录,postgresql,triggers,Postgresql,Triggers,这是我的桌子: ID Last_modified_date status 1 06-01-2013 17:05 New 1 06-02-2013 12:08 Assigned 2 07-03-2013 18:17 New 2 08-03-2013 15:12 Assigned 2 08-03-2013 18:05 Fixed 我需要以下结果 ID Last_modified_
ID Last_modified_date status
1 06-01-2013 17:05 New
1 06-02-2013 12:08 Assigned
2 07-03-2013 18:17 New
2 08-03-2013 15:12 Assigned
2 08-03-2013 18:05 Fixed
我需要以下结果
ID Last_modified_date status Trigger
1 06-01-2013 17:05 New False
1 06-02-2013 12:08 Assigned True
2 07-03-2013 18:17 New False
2 08-03-2013 15:12 Assigned False
2 08-03-2013 18:05 Fixed True
我可以使用此触发器检索每个ID的最后记录。此触发器将为您提供所需的结果,但我不确定这是否是您想要的逻辑:
with cte as (
select
*,
row_number() over(partition by id order by last_modified_date desc) as row_num
from Table1
)
select
id, last_modified_date, status,
case when row_num = 1 then 'True' else 'False' end as "Trigger"
from cte
order by id, last_modified_date asc
或者,如果您只需要每个id的最后一行,则可以使用distinct on:
更新
如果需要触发器,请尝试以下操作:
create or replace function tr_Table1_func()
returns trigger
as
$$
begin
update Table1 as t set
tr = t.last_modified_date > new.last_modified_date
where t.id = new.id and t.tr = TRUE
returning not tr into new.tr;
new.tr = coalesce(new.tr, TRUE);
return new;
end;
$$
language plpgsql;
create trigger tr_Table1
before insert on Table1
for each row execute procedure tr_Table1_func();
这一个会给你想要的结果,但我不确定这是你想要的逻辑:
with cte as (
select
*,
row_number() over(partition by id order by last_modified_date desc) as row_num
from Table1
)
select
id, last_modified_date, status,
case when row_num = 1 then 'True' else 'False' end as "Trigger"
from cte
order by id, last_modified_date asc
或者,如果您只需要每个id的最后一行,则可以使用distinct on:
更新
如果需要触发器,请尝试以下操作:
create or replace function tr_Table1_func()
returns trigger
as
$$
begin
update Table1 as t set
tr = t.last_modified_date > new.last_modified_date
where t.id = new.id and t.tr = TRUE
returning not tr into new.tr;
new.tr = coalesce(new.tr, TRUE);
return new;
end;
$$
language plpgsql;
create trigger tr_Table1
before insert on Table1
for each row execute procedure tr_Table1_func();
我不明白触发栏的意思。它基于哪些信息包含true或false`。此外,你对触发器的使用有点混乱。触发器是在数据库中发生更改时运行的一段代码,它不能在SELECT语句中使用。我需要为特定ID的每个新记录从true切换到false如何标识新记录?我真的看不到示例输出中的逻辑。我认为这意味着每个ID号的最新修改。有两行ID=1,只有最新的一行标记为true;同样,对于ID=2的三行,我不理解触发器列的名称。它基于哪些信息包含true或false`。此外,你对触发器的使用有点混乱。触发器是在数据库中发生更改时运行的一段代码,它不能在SELECT语句中使用。我需要为特定ID的每个新记录从true切换到false如何标识新记录?我真的看不到示例输出中的逻辑。我认为这意味着每个ID号的最新修改。有两行ID=1,只有最新的一行标记为true;同样,对于ID=2的三行,触发器列也是完全多余的。OP说他想使用触发器为每个ID检索最后修改的行。所以我只使用cte作为select*,row\u number在分区上按ID排序,最后修改的日期desc作为表1中的row\u num选择ID,moddate,来自cte的状态,其中rownum=1 order by id asc,应该做OP真正想要做的事情。@dcsohl是的,我会将此添加到答案中,谢谢。但由于OP不回答问题,所以很难知道他真正想要什么。如果我的回答是正确的,这个单词trigger应该从这个页面中完全删除,这与triggers没有任何关系。我真的很抱歉这么晚了。我刚看到你的答案。我真正想要的是在源表中添加一列。此列是一个触发器或标志,对于特定ID的每个新记录,该触发器或标志为true;对于旧记录,该触发器或标志为false。@user2311028是否确实要将其作为触发器?它可以在运行中用row_数计算,而且速度相对较快是的,我需要使用触发器。我需要检测每次插入的新记录,并将旧记录设置为false,将新记录设置为true。此解决方案仅在一次插入数据时使用。触发器列是完全多余的。OP说他想使用触发器为每个ID检索最后修改的行。所以我只使用cte作为select*,row\u number在分区上按ID排序,最后修改的日期desc作为表1中的row\u num选择ID,moddate,来自cte的状态,其中rownum=1 order by id asc,应该做OP真正想要做的事情。@dcsohl是的,我会将此添加到答案中,谢谢。但由于OP不回答问题,所以很难知道他真正想要什么。如果我的回答是正确的,这个单词trigger应该从这个页面中完全删除,这与triggers没有任何关系。我真的很抱歉这么晚了。我刚看到你的答案。我真正想要的是在源表中添加一列。此列是一个触发器或标志,对于特定ID的每个新记录,该触发器或标志为true;对于旧记录,该触发器或标志为false。@user2311028是否确实要将其作为触发器?它可以在运行中用row_数计算,而且速度相对较快是的,我需要使用触发器。我需要检测每次插入的新记录,并将旧记录设置为false,将新记录设置为true。只有在一次插入数据时才使用此解决方案。