join表,选择所有,但很少重命名sqlbq

mwecs4sa  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(305)

我想连接两个具有4个相同列名(id、fieldid、fieldcropyear、updateutc)的表。我想在fieldid上连接这两个表。
我使用了以下查询:

SELECT * FROM `xxx.yyy.111` AS ACT
    INNER JOIN `xxx.yyy.222` AS BOUND
    ON BOUND.FieldID = ACT.FieldID
    AND BOUND.FieldCropYear = ACT.FieldCropYear

运行后,出现以下错误: Duplicate column names in the result are not supported. Found duplicate(s): ID, FieldID, FieldCropYear, UpdateUTC 是否可以选择*但在一个Dataframe中重命名这4列?最有效的方法是什么?有没有可能是这样的:

SELECT * 
EXCEPT(ID, FieldID, UpdateUTC, FieldCropYear), 
                     ID as ID_b,
                     FieldID as FieldID_b,
                     UpdateUTC as UpdateUTC_b,
                     FieldCropYear as FieldCropYear_b
FROM `xxx.yyy.222` AS BOUND
    INNER JOIN `xxx.yyy.111` AS ACT
    ON BOUND.FieldID_b = ACT.FieldID
    AND BOUND.FieldCropYear_b = ACT.FieldCropYear
mklgxw1f

mklgxw1f1#

退房 using() 而不是 on -但是你必须使用所有的字段:

SELECT * FROM `xxx.yyy.111` AS ACT
    INNER JOIN `xxx.yyy.222` AS BOUND
    USING(ID, FieldID, FieldCropYear,UpdateUTC)

如果由于某些字段实际上不同而没有意义,请重命名子查询中的某些字段:

SELECT * FROM `xxx.yyy.111` AS ACT
    INNER JOIN 
     (select * EXCEPT(ID, UpdateUTC), ID AS actID, UpdateUTC as actUpdateUTC FROM `xxx.yyy.222`) AS BOUND
    USING(FieldID, FieldCropYear)

没有测试代码,但我希望方法是明确的

66bbxpm5

66bbxpm52#

bigquery使用 replace :

SELECT bound.*,
       act.* REPLACE (id as id_act,
                      filedid as fieldid_act,
                      UpdateUTC as UpdateUTC_act,
                      FieldCropYear as FieldCropYear_act
                     )
FROM `xxx.yyy.222` BOUND JOIN
     `xxx.yyy.111` AS ACT
     ON BOUND.FieldID_b = ACT.FieldID AND
        BOUND.FieldCropYear_b = ACT.FieldCropYear;

注意:当我想从具有重叠名称的表中选择列时,我通常会选择它们作为记录:

SELECT BOUND, ACT
FROM . . .

不过,我不确定这是否适合你。

相关问题