SQL 没有公共键的水平合并
SQL Union horizontally without common key
我在 BigQuery 中有两个表。
表 1:
| timestamp | FIELD1 | FIELD2 | ...
| 12345678 | 000000 | 000000 | ...
| 00154789 | 000000 | 000000 | ...
表 2:
| temperature |
| 1000000000 |
| 2000000000 |
需要SELECT输出
| timestamp | temperature | FIELD1 | FIELD2 | ...
| 12345678 | 1000000000 | 000000 | 000000 | ...
| 00154789 | 2000000000 | 000000 | 000000 | ...
我想简单地将两者水平合并。两个表之间没有公共键。
谢谢。
参考此 site,将其发布为 Wikianswer。
您可能想尝试这些查询以获得所需的结果。
Cross-join方法:
with table1 as (
select 12345678 as timestmp, 000000 as field1, 000000 as field2,
union all select 00154789 as timestmp, 000000 as field1, 000000 as field2
),
table2 as (
select 1000000000 as temperature,
union all select 2000000000 as temperature
)
select t1.timestmp,
t2.temperature,
t1.field1,
t1.field2
from table1 t1
cross join table2 t2
where
(t1.timestmp = 12345678 and t2.temperature = 1000000000)
or
(t1.timestmp = 154789 and t2.temperature = 2000000000)
行数方法:
with table1 as (
select 12345678 as timestmp, 000000 as field1, 000000 as field2,
union all select 00154789 as timestmp, 000000 as field1, 000000 as field2
),
table2 as (
select 1000000000 as temperature,
union all select 2000000000 as temperature
),
table1_row_num as (
select *, row_number() over (order by (timestmp)) as row_num from table1
),
table2_row_num as (
select *, row_number() over (order by (temperature) desc) as row_num from table2
)
select t1.timestmp,
t2.temperature,
t1.field1,
t1.field2
from table1_row_num as t1
inner join table2_row_num as t2
on t1.row_num = t2.row_num
两个查询的输出:
我在 BigQuery 中有两个表。
表 1:
| timestamp | FIELD1 | FIELD2 | ...
| 12345678 | 000000 | 000000 | ...
| 00154789 | 000000 | 000000 | ...
表 2:
| temperature |
| 1000000000 |
| 2000000000 |
需要SELECT输出
| timestamp | temperature | FIELD1 | FIELD2 | ...
| 12345678 | 1000000000 | 000000 | 000000 | ...
| 00154789 | 2000000000 | 000000 | 000000 | ...
我想简单地将两者水平合并。两个表之间没有公共键。
谢谢。
参考此 site,将其发布为 Wikianswer。
您可能想尝试这些查询以获得所需的结果。
Cross-join方法:
with table1 as (
select 12345678 as timestmp, 000000 as field1, 000000 as field2,
union all select 00154789 as timestmp, 000000 as field1, 000000 as field2
),
table2 as (
select 1000000000 as temperature,
union all select 2000000000 as temperature
)
select t1.timestmp,
t2.temperature,
t1.field1,
t1.field2
from table1 t1
cross join table2 t2
where
(t1.timestmp = 12345678 and t2.temperature = 1000000000)
or
(t1.timestmp = 154789 and t2.temperature = 2000000000)
行数方法:
with table1 as (
select 12345678 as timestmp, 000000 as field1, 000000 as field2,
union all select 00154789 as timestmp, 000000 as field1, 000000 as field2
),
table2 as (
select 1000000000 as temperature,
union all select 2000000000 as temperature
),
table1_row_num as (
select *, row_number() over (order by (timestmp)) as row_num from table1
),
table2_row_num as (
select *, row_number() over (order by (temperature) desc) as row_num from table2
)
select t1.timestmp,
t2.temperature,
t1.field1,
t1.field2
from table1_row_num as t1
inner join table2_row_num as t2
on t1.row_num = t2.row_num
两个查询的输出: