I need to update empty values in a table with values presents in another table. Shortly these values are ids of groups, and I need to pass these groupids in all events of my users to recognize in which group the user held this event. Example:
Inputs:
Table A:
event | timestamp | groupid | userid |
---|---|---|---|
created | 2021-03-09T09:58:17.198362522Z | c98 | |
updated | 2021-03-09T09:59:17.198362522Z | c98 | |
created | 2021-03-09T09:46:17.198362522Z | a32 | |
updated | 2021-03-09T10:20:17.198362522Z | c98 | |
updated | 2021-03-09T10:22:17.198362522Z | c98 | |
created | 2021-03-09T09:58:17.198362522Z | a32 | |
created | 2021-03-09T10:40:17.198362522Z | c98 |
Table B:
groupid | timestamp | userid |
---|---|---|
f13 | 2021-03-09T09:58:17.198362522Z | c98 |
f14 | 2021-03-09T10:15:17.198362522Z | c98 |
ad8 | 2021-03-09T09:45:00.198362522Z | a32 |
ad9 | 2021-03-09T09:58:17.198362522Z | a32 |
Desired Output:
Table A:
event | timestamp | groupid | userid |
---|---|---|---|
created | 2021-03-09T09:58:17.198362522Z | f13 | c98 |
updated | 2021-03-09T09:59:17.198362522Z | f13 | c98 |
created | 2021-03-09T09:46:17.198362522Z | ad8 | a32 |
updated | 2021-03-09T10:20:17.198362522Z | f14 | c98 |
updated | 2021-03-09T10:22:17.198362522Z | f14 | c98 |
created | 2021-03-09T09:59:17.198362522Z | ad9 | a32 |
created | 2021-03-09T10:40:17.198362522Z | f14 | c98 |
isn't it just a simple join?
update table1 a
set groupid = (select groupid from table2 b
where a.userid = b.userid
and a.timestamp >= b.timestamp
order by b.timestamp desc
limit 1
)
where a.groupid id null
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.