如何使用 Amazon Redshift 中的临时表列更新现有表中的列?

Posted

技术标签:

【中文标题】如何使用 Amazon Redshift 中的临时表列更新现有表中的列?【英文标题】:How to update columns in existing table by using temporary table columns in Amazon Redshift? 【发布时间】:2021-05-07 05:05:22 【问题描述】:

以下代码是用 SQL 开发的,用于更新目标表列。有人可以帮我在 redshift 中重写下面的查询,因为我试图在亚马逊 redshift 上执行相同的查询,它给出的错误如下:

亚马逊无效操作:关系“c”不存在;

With TempTable As
(
        SELECT   Left('abcdefghijk',len(TerritoryName)/3) +  Substring(TerritoryName,len(TerritoryName)-len(TerritoryName)/3-len(TerritoryName)/3+1,len(TerritoryName)-len(TerritoryName)/3-len(TerritoryName)/3) + Right('ijklmnopqrstuv',len(TerritoryName)/3) As Masked_TerritoryName
                ,Left('abcdefghijk',len(DistrictName)/3) +  Substring(DistrictName,len(DistrictName)-len(DistrictName)/3-len(DistrictName)/3+1,len(DistrictName)-len(DistrictName)/3-len(DistrictName)/3) + Right('ijklmnopqrstuv',len(DistrictName)/3) As Masked_DistrictName
                ,Left('abcdefghijk',len(RegionName)/3) +  Substring(RegionName,len(RegionName)-len(RegionName)/3-len(RegionName)/3+1,len(RegionName)-len(RegionName)/3-len(RegionName)/3) + Right('ijklmnopqrstuv',len(RegionName)/3) As Masked_RegionName
                ,Left('abcdefghijk',len(RSMTerritoryName)/3) +  Substring(RSMTerritoryName,len(RSMTerritoryName)-len(RSMTerritoryName)/3-len(RSMTerritoryName)/3+1,len(RSMTerritoryName)-len(RSMTerritoryName)/3-len(RSMTerritoryName)/3) + Right('ijklmnopqrstuv',len(RSMTerritoryName)/3) As Masked_RSMTerritoryName
                ,Left('abcdefghijk',len(CCAName)/3) +  Substring(CCAName,len(CCAName)-len(CCAName)/3-len(CCAName)/3+1,len(CCAName)-len(CCAName)/3-len(CCAName)/3) + Right('ijklmnopqrstuv',len(CCAName)/3) As Masked_CCAName
                ,Left('abcdefghijk',len(LCAName)/3) +  Substring(LCAName,len(LCAName)-len(LCAName)/3-len(LCAName)/3+1,len(LCAName)-len(LCAName)/3-len(LCAName)/3) + Right('ijklmnopqrstuv',len(LCAName)/3) As Masked_LCAName
                ,Left('abcdefghijk',len(TMComp)/3) +  Substring(TMComp,len(TMComp)-len(TMComp)/3-len(TMComp)/3+1,len(TMComp)-len(TMComp)/3-len(TMComp)/3) + Right('ijklmnopqrstuv',len(TMComp)/3) As Masked_TMComp
                ,Left('abcdefghijk',len(ASMTerritoryName)/3) +  Substring(ASMTerritoryName,len(ASMTerritoryName)-len(ASMTerritoryName)/3-len(ASMTerritoryName)/3+1,len(ASMTerritoryName)-len(ASMTerritoryName)/3-len(ASMTerritoryName)/3) + Right('ijklmnopqrstuv',len(ASMTerritoryName)/3) As Masked_ASMTerritoryName
                ,TerritoryCode      
        FROM TargetTable
)
    Update C
    Set      C.TerritoryName = N.Masked_TerritoryName
            ,C.DistrictName = N.Masked_DistrictName
            ,C.RegionName = N.Masked_RegionName
            ,C.RSMTerritoryName = N.Masked_RSMTerritoryName
            ,C.CCAName = N.Masked_CCAName
            ,C.LCAName = N.Masked_LCAName
            ,C.TMComp = N.Masked_TMComp
            ,C.ASMTerritoryName = N.Masked_ASMTerritoryName
    From TargetTable C
        Inner Join TempTable N ON C.TerritoryCode = N.TerritoryCode

【问题讨论】:

请只标记一个 RDBMS 【参考方案1】:

我不相信您可以只为目标表使用别名。您有“...更新 C ...”,我希望您需要“...更新 TargetTable ...”或“...更新 TargetTable C ...”。

您也不需要在 FROM 子句中列出 TargetTable,因为这是假定的。您的加入条件成为条件。因此,您的查询将如下所示:

With TempTable As
(
        SELECT ...
        FROM TargetTable
)
Update TargetTable C
    Set      ...
From TempTable N
Where C.TerritoryCode = N.TerritoryCode

【讨论】:

以上是关于如何使用 Amazon Redshift 中的临时表列更新现有表中的列?的主要内容,如果未能解决你的问题,请参考以下文章

如何更改 Amazon Redshift 中的默认时区?

如何解决 Amazon Redshift 中的数字溢出问题

没有函数或存储过程的 Amazon RedShift 中的 Upsert

使用 Avro 文件批量复制到 Amazon Redshift 中的异常处理

如何清除 Amazon Redshift 中的以下语法错误

限制 Amazon Redshift 中架构的大小