2列中的大查询枢轴多列

发布于 2025-01-24 14:18:20 字数 2111 浏览 0 评论 0 原文

我如何在大查询中进行转换/枢纽:

从此

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 
     

可能需要 unnest name 列以一种很好的方式。

如何创建名称列?

How can I transform/pivot in 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.

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:

    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.

How do I create the name column?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摘星┃星的人 2025-01-31 14:18:20

您可以使用为此:

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 也可以工作的想法,您只需要将 name value 保留在一个中单个数组:

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

You can use the UNPIVOT operator for this:

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:

SELECT solution, sentiment, `groups`, name, value
FROM t, 
UNNEST (
    ARRAY<STRUCT<name STRING, value BOOLEAN>>[('feeling', feeling), ('playing', playing), ('doing', doing)]
) ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文