Big Query在2列中透视多个列



如何在大查询中转换/pivot:

从这个:

solution                   sentiment     groups                feeling  playing doing 
I am good                  positive    ['good', 'am']            1        0      0
I am playing               positive    ['playing', 'am']         0        1      1
She is running             positive    ['running', 'she]         0        1      0
He is not eating           negative    ['eating']                1        0      1

:

solution                   sentiment     groups                    name     value
I am good                  positive    ['good', 'am']              feeling    1
I am good                  positive    ['good', 'am']              playing    0
I am good                  positive    ['good', 'am']              doing      0
I am playing               positive    ['playing', 'am']           feeling    0
I am playing               positive    ['playing', 'am']           playing    1
I am playing               positive    ['playing', 'am']           doing      1
She is running             positive    ['running', 'she]           feeling     0
She is running             positive    ['running', 'she]           playing     1
She is running             positive    ['running', 'she]           doing       1
He is not eating           negative    ['eating']                  feeling     1
He is not eating           negative    ['eating']                  playing     0
He is not eating           negative    ['eating']                  doing       1

我试过这种方式,但我错过了name列…休息得很好。

SELECT solution, sentiment, groups, value
FROM table
LEFT JOIN UNNEST ([feeling, playing doing] ) AS value 

我已经尝试过这样得到name列,但不工作,因为它给出了错误的结果:

SELECT solution, sentiment, groups, value, name
FROM table, 
UNNEST (['feeling', 'playing','doing']) AS name
LEFT JOIN UNNEST ([feeling, playing, doing] ) AS value 

可能需要UNNESTname列在一个很好的方式。

如何创建name列?

您可以使用UNPIVOT操作符:

CREATE TEMP TABLE t (
solution STRING,
sentiment STRING,
`groups` ARRAY<STRING>,
feeling BOOLEAN,
playing BOOLEAN,
doing BOOLEAN
);
INSERT INTO t
(solution, sentiment, `groups`, feeling, playing, doing)
VALUES
('I am good', 'positive', ['good', 'am'], true, false, false),
('I am playing', 'positive', ['playing', 'am'], false, true, true),
('She is running', 'positive', ['running', 'she'], false, true, false),
('He is not eating', 'negative', ['eating'], true, false, true);
SELECT *
FROM t UNPIVOT(value FOR name IN (feeling, playing, doing));

返回
solution    sentiment   groups  value   name
He is not eating    negative    [eating]    true    feeling
He is not eating    negative    [eating]    false   playing
He is not eating    negative    [eating]    true    doing
I am good   positive    "[good,am]" true    feeling
I am good   positive    "[good,am]" false   playing
I am good   positive    "[good,am]" false   doing
She is running  positive    "[running,she]" false   feeling
She is running  positive    "[running,she]" true    playing
She is running  positive    "[running,she]" false   doing
I am playing    positive    "[playing,am]"  false   feeling
I am playing    positive    "[playing,am]"  true    playing
I am playing    positive    "[playing,am]"  true    doing

您使用UNNEST的想法也可以工作,您只需要将namevalue保持在单个数组中:

SELECT solution, sentiment, `groups`, name, value
FROM t, 
UNNEST (
ARRAY<STRUCT<name STRING, value BOOLEAN>>[('feeling', feeling), ('playing', playing), ('doing', doing)]
) ;

最新更新