[英]Big Query Pivot multiple columns in 2 columns
How can I transform/pivot in Big Query:我如何在 Big Query 中进行转换/透视:
From this:由此:
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
To:到:
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
I tried this way, but I am missing the name
column...rest is looking fine.我试过这种方式,但我缺少name
列...rest 看起来不错。
SELECT solution, sentiment, groups, value
FROM table
LEFT JOIN UNNEST ([feeling, playing doing] ) AS value
I have tried like this to get the name
column but doesn't work as it is giving wrong results:我试过这样获取name
列但不起作用,因为它给出了错误的结果:
SELECT solution, sentiment, groups, value, name
FROM table,
UNNEST (['feeling', 'playing','doing']) AS name
LEFT JOIN UNNEST ([feeling, playing, doing] ) AS value
Might need to UNNEST
the name
column in a nice way.可能需要以一种很好的方式UNNEST
name
列。
How do I create the name
column?如何创建name
列?
You can use the UNPIVOT
operator for this:您可以为此使用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));
returns回报
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
Your idea of using UNNEST
can also work, you just need to keep both name
and value
in a single array:您使用UNNEST
的想法也可以奏效,您只需要将name
和value
都保存在一个数组中:
SELECT solution, sentiment, `groups`, name, value
FROM t,
UNNEST (
ARRAY<STRUCT<name STRING, value BOOLEAN>>[('feeling', feeling), ('playing', playing), ('doing', doing)]
) ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.