[英]SQL to add an entire column to another column with duplicated values of rest of the columns?
我写了一个 Bigquery SQL 来生成以下内容:
id1 id2 orders price
x1 y1 100 1
x2 y2 200 2
x3 y3 300 3
预期的:
id1 orders price
x1 100 1
x2 200 2
x3 300 3
y1 100 1
y2 200 2
y3 300 3
这可以使用union
来实现
select id1, orders, price from test
union all
select id2, orders, price from test
以下是 BigQuery 标准 SQL
#standardSQL
SELECT id1, orders, price
FROM `project.dataset.table`,
UNNEST([id1, id2]) id1
您可以使用您问题中的示例数据进行测试,使用上面的示例数据,如下例所示
#standardSQL
WITH `project.dataset.table` AS (
SELECT 'x1' id1, 'y1' id2, 100 orders, 1 price UNION ALL
SELECT 'x2', 'y2', 200, 2 UNION ALL
SELECT 'x3', 'y3', 300, 3
)
SELECT id1, orders, price
FROM `project.dataset.table`,
UNNEST([id1, id2]) id1
-- ORDER BY id1
结果
Row id1 orders price
1 x1 100 1
2 x2 200 2
3 x3 300 3
4 y1 100 1
5 y2 200 2
6 y3 300 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.