我有表A ,其中一列City看起来像这样(前10行):
城市
Torrance
San Carlos
San Jose
Walnut Creek
Carmichael
Anderson
Altadena
Orange
Palos Verdes Estates
和表B ,其中一列名称是这样的(前10行):
名称
Merrill
Elm
Blossom Hill
Brandywine
Altamont
Hill
Wapello
Palmyra
Via Almar
我需要获取一个新的表C ,其中包含2列城市和名称 从表A的前1,000行和表B的前1,000行开始。
结果表应如下所示(前10行):
城市 ................................. ... 名称
Torrance Merrill
San Carlos Elm
San Jose Blossom Hill
Walnut Creek Brandywine
Carmichael Altamont
Anderson Hill
Altadena Wapello
Orange Palmyra
Palos Verdes Estates Via Almar
问题是如何水平连接每个表的前1,000行?
答案 0 :(得分:1)
以下是BigQuery标准SQL的内容(另请参见您问题下方的我的评论)
#standardSQL
SELECT City, Name FROM (
SELECT City, ROW_NUMBER() OVER() pos
FROM `project.dataset.tableA` LIMIT 1000
) a JOIN (
SELECT Name, ROW_NUMBER() OVER() pos
FROM `project.dataset.tableB` LIMIT 1000
) b USING(pos)
如果要应用到您的问题中的示例数据,如下面的示例
#standardSQL
WITH `project.dataset.tableA` AS (
SELECT 'Torrance' City UNION ALL
SELECT 'San Carlos' UNION ALL
SELECT 'San Jose' UNION ALL
SELECT 'Walnut Creek' UNION ALL
SELECT 'Carmichael' UNION ALL
SELECT 'Anderson' UNION ALL
SELECT 'Altadena' UNION ALL
SELECT 'Orange' UNION ALL
SELECT 'Palos Verdes Estates'
), `project.dataset.tableB` AS (
SELECT 'Merrill' Name UNION ALL
SELECT 'Elm' UNION ALL
SELECT 'Blossom Hill' UNION ALL
SELECT 'Brandywine' UNION ALL
SELECT 'Altamont' UNION ALL
SELECT 'Hill' UNION ALL
SELECT 'Wapello' UNION ALL
SELECT 'Palmyra' UNION ALL
SELECT 'Via Almar'
)
SELECT City, Name FROM (
SELECT City, ROW_NUMBER() OVER() pos
FROM `project.dataset.tableA` LIMIT 1000
) a JOIN (
SELECT Name, ROW_NUMBER() OVER() pos
FROM `project.dataset.tableB` LIMIT 1000
) b USING(pos)
结果是
Row City Name
1 Torrance Merrill
2 San Carlos Elm
3 San Jose Blossom Hill
4 Walnut Creek Brandywine
5 Carmichael Altamont
6 Anderson Hill
7 Altadena Wapello
8 Orange Palmyra
9 Palos Verdes Estates Via Almar