有没有办法将列旋转到雪花中的行?

时间:2021-03-31 21:44:39

标签: multiple-columns snowflake-cloud-data-platform unpivot

我有一个像下面这样的单行表,它来自像下面这样的雪花查询

show warehouses like 'COMMON_WH';
select "name","state","type","size" FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()))

name|state|type|size
COMMON_WH|SUSPENDED|STANDARD|Small

我想像下面那样旋转它。

name| COMMON_WH
state|SUSPENDED
type|STANDARD
size|Small

谢谢,

1 个答案:

答案 0 :(得分:1)

您可以使用 unpivot 执行此操作。这是一个如何使用它的示例。请注意,它有点冗长,因为您需要在逆透视之前将所有原始表的数据类型转换为相同的数据类型(在本例中为 varchar)。

show warehouses like 'COMMON_WH';

select *
from (
    select
        "name"::varchar  as name,
        "state"::varchar as state,
        "type"::varchar  as type,
        "size"::varchar  as size
    FROM TABLE (RESULT_SCAN(LAST_QUERY_ID()))
) unpivot (col_val for col_name in (name, state, type, size))

这会产生:

<头>
COL_NAME COL_VAL
NAME COMMON_WH
STATE 开始
TYPE 标准
SIZE X-小