在BigQuery中创建可展平表格的视图

时间:2018-07-13 13:01:19

标签: sql google-bigquery

我在BigQuery中有一个表,该表以以下结构显示数据:

+------------+---------+-----------+-----------+
|    Date    | Product | CostPrice | SalePrice |
+------------+---------+-----------+-----------+
| 01/01/2018 | hat     |        10 |        20 |
| 01/01/2018 | shoe    |         5 |        12 |
| 01/02/2018 | hat     |        11 |        22 |
| 01/02/2018 | shoe    |         6 |        13 |
+------------+---------+-----------+-----------+

我想弄平(这是正确的术语吗?)数据,以便每一行只有一个度量。显然,这需要MetricName列来确定每行中提供的是第一个表中的哪个度量。

+------------+-------------+------------+-------------+
|    Date    | Product     | MetricName | MetricValue |
+------------+-------------+------------+-------------+
| 01/01/2018 | hat         | CostPrice  |          10 |
| 01/01/2018 | hat         | SalePrice  |          20 |
| 01/01/2018 | shoe        | CostPrice  |           5 |
| 01/01/2018 | shoe        | SalePrice  |          12 |
| 01/02/2018 | hat         | CostPrice  |          11 |
| 01/02/2018 | hat         | SalePrice  |          22 |
| 01/02/2018 | shoe        | CostPrice  |           6 |
| 01/02/2018 | shoe        | SalePrice  |          13 |
+------------+-------------+------------+-------------+

我不确定如何构建可实现此目标的SQL查询?

谢谢

J.Ayo

1 个答案:

答案 0 :(得分:4)

以下是用于BigQuery标准SQL

#standardSQL
SELECT Date, Product, 'CostPrice' AS MetricName, CostPrice AS MetricValue
FROM `project.dataset.table` UNION ALL
SELECT Date, Product, 'SalePrice' AS MetricName, SalePrice AS MetricValue
FROM `project.dataset.table`    

您可以使用下面的问题中的虚拟数据进行测试,操作

#standardSQL
WITH `project.dataset.table` AS (
  SELECT '01/01/2018' Date, 'hat' Product, 10 CostPrice, 20 SalePrice UNION ALL
  SELECT '01/01/2018', 'shoe', 5, 12 UNION ALL
  SELECT '01/02/2018', 'hat', 11, 22 UNION ALL
  SELECT '01/02/2018', 'shoe', 6, 13 
)
SELECT Date, Product, 'CostPrice' AS MetricName, CostPrice AS MetricValue
FROM `project.dataset.table` UNION ALL
SELECT Date, Product, 'SalePrice', SalePrice
FROM `project.dataset.table`   

另一个避免使用UNION ALL的选项(因为它需要一些冗余代码)如下

#standardSQL
SELECT DATE, Product, MetricName, MetricValue
FROM `project.dataset.table`,
UNNEST([STRUCT<MetricName STRING, MetricValue INT64>
  ('CostPrice', CostPrice), ('SalePrice', SalePrice)])