我有两个下表。我想保留t1以外的所有内容,以及t2以外的所有其他内容,除了Date,Id。两个表的指标值(工资,奖金)均不得更改。由于t1中有多个日期和ID,因此输出中出现重复。我的代码如下。请协助。
从t1中选择t1。*,t2。* except(Date,Id) 左加入t2 在t1.Date = t2。日期 和t1.Id = t2.Id
答案 0 :(得分:0)
虽然输出表的逻辑可能缺乏解释,但我可以回答您的主要问题。您可以使用LEFT JOIN并简单地手动SELECT从每个表中获取所需的所有列,并以所需的顺序将其写入。
下面是我创建的一些示例数据的语法。
with t1 as (
SELECT DATE(2020,01,22) as Date, 1 as id, "abc" as Name, "NYC" as City, "USA" as Country, 5000 as Salary UNION ALL
SELECT DATE(2020,01,23) as Date, 2 as id, "abc" as Name, "SF" as City, "USA" as Country, 8000 as Salary UNION ALL
SELECT DATE(2020,01,22) as Date, 2 as id, "abc" as Name, "SF" as City, "USA" as Country, 8000 as Salary
),
t2 as (
SELECT DATE(2020,01,22) as Date, 1 as id, "Man" as Position, "1st" as Rank, 1000 as Bonus UNION ALL
SELECT DATE(2020,01,22) as Date, 2 as id, "Man" as Position, "1st" as Rank, 1000 as Bonus
)
SELECT t1.Date, t1.id, t1.Name, t1.City, t2.Position, t2.Rank, t1.Country, t1.Salary,
t2.Bonus
FROM t1 LEFT JOIN t2 on t1.Date=t2.Date and t1.id=t2.id
输出,
请注意,我已经按照希望它们在输出中的顺序选择了列。此外,Date
和id
来自select语句中指定的表1。另一个重要的一点是,在t1.Date=t2.Date and t1.id=t2.id
未被检查为true的情况下,分配给t2
的列的值为null
。
我想指出的是,我只是作为示例手动设置了Rank
列的值。最后,with()
中的所有内容都是示例数据。