如何使数组由普通列值组成?

时间:2019-12-03 13:31:50

标签: sql google-bigquery

我有一个表格 A ,如下所示:

|   title     | rating |
|-------------|--------|
|  predator   |   17   |
| tomb raider |   22   |
|  predator   |   18   |
|     ...     |  ...   |
|  predator   |   19   |
| tomb raider |   23   |

和另一个如下表 B

|   title     | rating |
|-------------|--------|
|  predator   |   31   |
| tomb raider |   41   |
|  predator   |   32   |
|     ...     |  ...   |
|  predator   |   19   |
| tomb raider |   23   |

如何创建具有A和B所有电影的收视率的下表,如果其他表中不存在收视率,则显示NULL,否则显示收视率?

我已经弄清楚如何找到应该是NULL的东西,但是我不知道如何制作数组。下表只有2行,捕食者1行,古墓丽影1行。其余的嵌套。

|   title     | rating A | rating B |
|-------------|----------|----------|
|  predator   |    17    |   NULL   |
|             |    18    |   NULL   |
|             |    19    |    19    |
|             |   NULL   |    31    |
|             |   NULL   |    32    |
|             |   ...    |   ...    |
| tomb raider |    22    |   NULL   |
|             |    23    |    23    |
|             |   NULL   |    41    |
|             |   ...    |   ...    |

2 个答案:

答案 0 :(得分:1)

诀窍是先做GROUP BY COALESCE()然后做ARRAY_AGG(STRUCT())

WITH data AS (
  SELECT *, fhoffa.x.int(LOG(word_count)) cc
  FROM `bigquery-public-data.samples.shakespeare`
  WHERE corpus IN ('sonnets', 'kinghenryviii')
  AND word LIKE 'a%'
  AND word_count > 10
)

SELECT word, ARRAY_AGG(STRUCT(a,b)) ratings
FROM (
  SELECT word, MAX(a) a, MAX(b) b
  FROM (
    SELECT word
      , IF(corpus='sonnets', cc, null) a
      , IF(corpus='kinghenryviii', cc, null) b
    FROM data
  )
  GROUP BY word, COALESCE(a,b)
)
GROUP BY word

enter image description here

答案 1 :(得分:0)

以下是用于BigQuery标准SQL

#standardSQL
SELECT title, 
  ARRAY(
    SELECT AS STRUCT ratingA, ratingB
    FROM (SELECT DISTINCT rating FROM UNNEST(ARRAY_CONCAT(ratingA, ratingB)) rating ORDER BY rating) 
    LEFT JOIN UNNEST(ratingA) ratingA ON rating = ratingA
    LEFT JOIN UNNEST(ratingB) ratingB ON rating = ratingB
  ) ratings
FROM (SELECT title, ARRAY_AGG(rating ORDER BY rating) AS ratingA FROM `project.dataset.tableA` GROUP BY title) a
FULL OUTER JOIN (SELECT title, ARRAY_AGG(rating ORDER BY rating) AS ratingB FROM `project.dataset.tableB` GROUP BY title) b
USING(title)   

如果要应用于您的问题的样本数据-结果为

enter image description here