【发布时间】:2021-12-07 09:17:27
【问题描述】:
我有两个相似的表格想要加入。请参阅下面的可重现示例。
需要做什么
查看代码中的 cmets:连接值 '2021-01-01'(列:日期)、'hat'(列:内容)、'cat'(列:内容)和 'A'(列:Tote)在 first_table 中将产生一个唯一键,该键可以与 second_table 中完全相同的数据连接。结果将是 4 个唯一事件的第一行(请参阅 desired_result: '#first tote')。实际上,行数将是几百万。
可重现的例子:
CREATE OR REPLACE TABLE
`first_table` (
`Date` string NOT NULL,
`TotearrivalTimestamp` string NOT NULL,
`Tote` string NOT NULL,
`content` string NOT NULL,
`location` string NOT NULL,
);
INSERT INTO `first_table` (`Date`, `TotearrivalTimestamp`, `Tote`, `content`, `location`) VALUES
('2021-01-01', '13:00','A','hat','1'), #first tote
('2021-01-01', '13:00','A','cat','1'), #first tote
('2021-01-01', '14:00', 'B', 'toy', '1'),
('2021-01-01', '14:00', 'B', 'cat', '1'),
('2021-01-01', '15:00', 'A', 'toy', '1'),
('2021-01-01', '13:00', 'A', 'toy', '1'),
('2021-01-02', '13:00', 'A', 'hat', '1'),
('2021-01-02', '13:00', 'A', 'cat', '1');
CREATE OR REPLACE TABLE
`second_table` (
`Date` string NOT NULL,
`ToteendingTimestamp` string NOT NULL,
`Tote` string NOT NULL,
`content` string NOT NULL,
`location` string NOT NULL,
);
INSERT INTO `second_table` (`Date`, `ToteendingTimestamp`, `Tote`, `content`, `location`) VALUES
('2021-01-01', '20:00', 'B', 'cat', '2'),
('2021-01-01', '19:00', 'A', 'cat', '1'), #first tote
('2021-01-01', '19:00', 'A', 'hat', '1'), #first tote
('2021-01-01', '20:00', 'B', 'toy', '2'),
('2021-01-01', '14:00', 'A', 'toy', '1'),
('2021-01-02', '14:00', 'A', 'hat', '1'),
('2021-01-02', '14:00', 'A', 'cat', '1'),
('2021-01-01', '16:00', 'A', 'toy', '1');
CREATE OR REPLACE TABLE
`desired_result` (
`Date` string NOT NULL,
`Tote` string NOT NULL,
`TotearrivalTimestamp` string NOT NULL,
`ToteendingTimestamp` string NOT NULL,
`location_first_table` string NOT NULL,
`location_second_table` string NOT NULL,
);
INSERT INTO `desired_result` (`Date`, `Tote`, `TotearrivalTimestamp`, `ToteendingTimestamp`, `location_first_table`, `location_second_table`) VALUES
('2021-01-01', 'A', '13:00', '19:00', '1', '1'), #first tote
('2021-01-01', 'B', '14:00', '20:00', '1', '1'),
('2021-01-01', 'A', '15:00', '16:00', '1', '2'),
('2021-01-02', 'A', '13:00', '14:00', '1', '1');
#### this does not give what I want####
select first.date as Date, first.tote, first.totearrivaltimestamp, second.toteendingtimestamp, first.location as location_first_table, second.location as location_second_table
from `first_table` first
inner join `second_table` second
on first.tote = second.tote
and first.content = second.content;
【问题讨论】:
-
对于所需的输出如何成为输入的函数没有明确的描述。 “基于”和“结合”没有告诉我们任何信息,也没有列出可能涉及的运营商。 minimal reproducible example 使用足够多的单词、句子和对部分示例的引用来清楚完整地表达你的意思。在给出业务关系(船)/关联或表(基础或查询结果)时,说明其中的一行根据其列值说明了业务情况。 PS 既然您将输入作为表格代码(好),您不需要早期的冗余(可能是错误的)版本。
-
请根据我最后的评论采取行动。没有人可以接受您所写的内容并知道要写什么查询。他们只能猜测。 PS 句子片段标记行不是“使用足够的单词、句子和对部分示例的引用来清楚完整地表达你的意思。”
-
好的,感谢您的支持。现在清楚了吗?
标签: sql join group-by google-bigquery