如何使用 postgresql 查找重复记录和更新?
How to find duplicate records and update using postgresql?
id
userid
deviceid
isactive
last_modified (timestamp)
1
12
fdghfgh
true
2021-02-12
2
12
sdsdfg
true
2021-02-14
3
5
fghfgh
true
2021-01-12
4
15
dffdg
true
2021-02-14
5
15
dofghfjdog
true
2021-01-09
一个用户只能使用一台设备。以上 table 用户 12 和 15 有两个活动设备。
如何在postgresql查询中将最近修改的设备设置为active,将其他设备设置为false(对应用户)?
结果应该是:
id
userid
deviceid
isactive
last_modified (timestamp)
1
12
fdghfgh
false
2021-02-12
2
12
sdsdfg
true
2021-02-14
3
5
fghfgh
true
2021-01-12
4
15
dffdg
true
2021-02-14
5
15
dofghfjdog
false
2021-01-09
您可以像下面这样使用 RANK () OVER 函数
这将为您提供根据每个用户 ID 组的最后修改日期排名的每个条目。
然后您可以编写更新查询,将 isactive 更新为 false device_rank! =1
select id,userid,deviceid,isactive,last_modified,
RANK () OVER (
PARTITION BY userid
ORDER BY last_modified DESC
) device_rank
from deviceTable
如果您想设置 值,请使用update
。子查询用于计算应被视为活动的日期:
update t
set is_active = (last_modified = max_lst_modified)
from (select user_id, max(last_modified) as max_last_modified
from t
group by user_id
) tt
where tt.user_id = t.user_id;
查询 1:(按 last_modified_time 对设备进行排名)
select id,userid,deviceid,isactive,last_modified_timestamp,
RANK () OVER (
PARTITION BY user_id
ORDER BY last_modified_timestamp DESC
) device_rank
from myschema.mytable rankTable
查询 2:(更新设备 table 使只有一个活动设备 - 最近的设备)
UPDATE myschema.mytable ud
SET is_active = false
FROM (select id,userid,deviceid,isactive,last_modified_timestamp,
RANK () OVER (
PARTITION BY user_id
ORDER BY last_modified_timestamp DESC
) device_rank
from myschema.mytable) as rankTable
WHERE ud.id=rankTable.id and rankTable.device_rank != 1;
id | userid | deviceid | isactive | last_modified (timestamp) |
---|---|---|---|---|
1 | 12 | fdghfgh | true | 2021-02-12 |
2 | 12 | sdsdfg | true | 2021-02-14 |
3 | 5 | fghfgh | true | 2021-01-12 |
4 | 15 | dffdg | true | 2021-02-14 |
5 | 15 | dofghfjdog | true | 2021-01-09 |
一个用户只能使用一台设备。以上 table 用户 12 和 15 有两个活动设备。
如何在postgresql查询中将最近修改的设备设置为active,将其他设备设置为false(对应用户)?
结果应该是:
id | userid | deviceid | isactive | last_modified (timestamp) |
---|---|---|---|---|
1 | 12 | fdghfgh | false | 2021-02-12 |
2 | 12 | sdsdfg | true | 2021-02-14 |
3 | 5 | fghfgh | true | 2021-01-12 |
4 | 15 | dffdg | true | 2021-02-14 |
5 | 15 | dofghfjdog | false | 2021-01-09 |
您可以像下面这样使用 RANK () OVER 函数
这将为您提供根据每个用户 ID 组的最后修改日期排名的每个条目。
然后您可以编写更新查询,将 isactive 更新为 false device_rank! =1
select id,userid,deviceid,isactive,last_modified,
RANK () OVER (
PARTITION BY userid
ORDER BY last_modified DESC
) device_rank
from deviceTable
如果您想设置 值,请使用update
。子查询用于计算应被视为活动的日期:
update t
set is_active = (last_modified = max_lst_modified)
from (select user_id, max(last_modified) as max_last_modified
from t
group by user_id
) tt
where tt.user_id = t.user_id;
查询 1:(按 last_modified_time 对设备进行排名)
select id,userid,deviceid,isactive,last_modified_timestamp,
RANK () OVER (
PARTITION BY user_id
ORDER BY last_modified_timestamp DESC
) device_rank
from myschema.mytable rankTable
查询 2:(更新设备 table 使只有一个活动设备 - 最近的设备)
UPDATE myschema.mytable ud
SET is_active = false
FROM (select id,userid,deviceid,isactive,last_modified_timestamp,
RANK () OVER (
PARTITION BY user_id
ORDER BY last_modified_timestamp DESC
) device_rank
from myschema.mytable) as rankTable
WHERE ud.id=rankTable.id and rankTable.device_rank != 1;