如何在presto中将列转换为行?

2024-05-13 02:06:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在研究presto sql,结果被绊倒了

我想将列转换为新行并保存值

例如:

以前

NAME  COMDEDY  HORROR  ROMANCE
brian    10     20     14
tom      20     10     11

之后

NAME    GANRE    RATING
brian   comedy     10
brian   horror     20
brian   romance    14
tom     comedy     20
tom     horror     10
tom     romance    11

如果用prestodb做不到,我至少需要用python熊猫来做这件事

谢谢


Tags: namesqlratingtomprestobrianprestodbcomedy
1条回答
网友
1楼 · 发布于 2024-05-13 02:06:02

您可以使用presto sql执行此操作:

SELECT t1.name, t2.genre, t2.rating
FROM table t1
CROSS JOIN unnest (
  array['comedy', 'horror', 'romance'],
  array[comedy, horror, romance]
) t2 (genre, rating)

相关问题 更多 >