如何将表转换为枢轴(或转置)表?
我有这样的桌子: Table1:
month pool_name apy
------------------------------
04-2021 a 1
04-2021 c 2
04-2021 d 1
04-2021 f 3
04-2021 g 5
04-2021 h 6
05-2021 a 2
05-2021 b 3
05-2021 c 2
05-2021 e 7
05-2021 f 5
05-2021 h 6
05-2021 i 4
.
.
.
如何转置或枢轴(Actully我不知道)Table1至Table2:
month a b c d e f g h i
----------------------------------------------------
04-2021 1 0 2 1 0 3 5 6 0
05-2021 2 3 2 0 7 5 0 6 4
我使用此代码,但有错误。
select month,pool_name,apy from table1
pivot (sum(apy) for pool_name in (select distinct(pool_name) from table1))
我必须在Snowflake(经典的Web界面)中编写此代码。 先感谢您。
I have a table like this:
table1:
month pool_name apy
------------------------------
04-2021 a 1
04-2021 c 2
04-2021 d 1
04-2021 f 3
04-2021 g 5
04-2021 h 6
05-2021 a 2
05-2021 b 3
05-2021 c 2
05-2021 e 7
05-2021 f 5
05-2021 h 6
05-2021 i 4
.
.
.
how can I transpose or pivot (actully I don't know about that) the table1 to table2:
month a b c d e f g h i
----------------------------------------------------
04-2021 1 0 2 1 0 3 5 6 0
05-2021 2 3 2 0 7 5 0 6 4
I use this code but I have an error.
select month,pool_name,apy from table1
pivot (sum(apy) for pool_name in (select distinct(pool_name) from table1))
I have to write this code in snowflake (classic web interface).
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于动态枢轴,您需要使用过程或脚本。
表上的动态枢轴(列 - “ pool_name”):
接收到的主要查询
输出 -
使用的数据集 -
For Dynamic pivot you need to use a procedure or script.
Dynamic pivot on table (column - "pool_name"):
Main Query
Output received -
Data-set used -