简体   繁体   English

我如何编写一个 PostgreSQL 和 Python 命令从清理这个原始表到结果表,如下所示?

[英]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)

Online example在线示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM