[英]How can I write a PostgreSQL and Python command from cleaning this original table to result table as shown below?
In PostgreSQL, I tried the function trim(unnest(string_to_array
to clean and distribute to rows but it will not distribute each value to each other (user_id and dates).在 PostgreSQL 中,我尝试了 function
trim(unnest(string_to_array
来清理和分发到行,但它不会将每个值分发给彼此(user_id 和日期)。
Assuming that dates
is defined as jsonb
and user_id
is defined as int[]
you can do something like this:假设
dates
被定义为jsonb
并且user_id
被定义为int[]
你可以这样做:
select u.user_id,
d.dates,
t."type",
t.time_start,
t.time_end
from the_table t
cross join unnest(t.user_id) as u(user_id)
cross join jsonb_array_elements_text(t.dates) as d(dates)
order by u.user_id, d.date
If dates
is not a jsonb
column, you need to cast it: jsonb_array_elements_text(t.dates::jsonb)
如果
dates
不是jsonb
列,则需要对其进行转换: jsonb_array_elements_text(t.dates::jsonb)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.