【问题标题】:Need to update several different columns in one table from only one column found in another table需要从另一个表中找到的一列更新一个表中的多个不同列
【发布时间】:2021-12-16 12:30:06
【问题描述】:

我有两张表,TBL_A 和 TBL_B。

TBL_A 如下所示:

TBL_B 如下所示:

我需要根据在 TBL_A 中找到的与特定状态相对应的 DATETIME 信息更新 TBL_B,因此:

 when status = 'CLAIMED' put DATETIME in CLAIMED_DATETIME
 when status = 'BOUGHT' put DATETIME in BOUGHT_DATETIME
 when status = 'RETURNED' put DATETIME in RETURNED_DATETIME

我有这个问题:

   MERGE INTO TBL_B B
USING (
      SELECT * FROM TBL_A
    PIVOT(MIN(ACCOUNT_REWARD_UPDATED_AT_DATETIME) FOR ACCOUNT_REWARD_STATUS IN ('claimed', 'bought', 'returned')) 
     AS P (number_id, country, amount, currency, file_tp, ingestion_time, claimed, bought, returned)
) A ON (
       A.number_id = B.number_id, 
    and  A.country = B.country, 
    and A.amount = B.amount, 
    and A.currency = B.currency, 
    and A.file_tp = B.file_tp 
    and A.ingestion_time = B.ingestion_time
 )
WHEN MATCHED THEN
  UPDATE SET
    B.CLAIMED_DATETIME = IFF(A.claimed IS NOT NULL, A.claimable , '2999-12-31 23:59:5'),
    B.BOUGHT_DATETIME = IFF(A.bought IS NOT NULL, A.claimed , '2999-12-31 23:59:5'),
    B.RETURNED_DATETIME = IFF(A.reverted IS NOT NULL, A.reverted, '2999-12-31 23:59:5'); 

当我加入 ALL 列时,file_tp 和 ingestion_time 对于每个状态(认领、购买和退回)都是不同的,而其余列对于每个状态都是相同的。当我运行上面的查询时,它只更新 1 个 DATE,而不是全部。我意识到这是因为我无法加入 FILE_TP 和 INGESTION_TIME,因为每个状态都不同。但是,当我从连接条件中删除这些时,我收到一个错误:

   MERGE INTO TBL_B B
USING (
      SELECT * FROM TBL_A
    PIVOT(MIN(DATETIME) FOR STATUS IN ('claimed', 'bought', 'returned')) 
     AS P (number_id, country, amount, currency, file_tp, ingestion_time, claimed, bought, returned)
) A ON (
       A.number_id = B.number_id, 
    and  A.country = B.country, 
    and A.amount = B.amount, 
    and A.currency = B.currency, 
    --and A.file_tp = B.file_tp 
    --and A.ingestion_time = B.ingestion_time
 )
WHEN MATCHED THEN
  UPDATE SET
    B.CLAIMED_DATETIME = IFF(A.claimed IS NOT NULL, A.claimable , '2999-12-31 23:59:5'),
    B.BOUGHT_DATETIME = IFF(A.bought IS NOT NULL, A.claimed , '2999-12-31 23:59:5'),
    B.RETURNED_DATETIME = IFF(A.reverted IS NOT NULL, A.reverted, '2999-12-31 23:59:5'); 

错误:

Duplicate row detected during DML action Row Values: 

有谁知道我该如何解决这个问题?

【问题讨论】:

  • “当我运行上面的查询时,它只更新 1 个 DATE,而不是全部”——你的意思是有多个行需要更新,它们共享相同的 (number_id, country 、金额、货币)?

标签: sql snowflake-cloud-data-platform snowflake-schema


【解决方案1】:

我怀疑枢轴没有按照您的预期做 - 检查它的结果。

尝试改变这个:

      SELECT * FROM TBL_A
    PIVOT(MIN(DATETIME) FOR STATUS IN ('claimed', 'bought', 'returned')) 
     AS P (number_id, country, amount, currency, file_tp, ingestion_time, claimed, bought, returned)
) A ON (
       A.number_id = B.number_id, 
    and  A.country = B.country, 
    and A.amount = B.amount, 
    and A.currency = B.currency, 
    --and A.file_tp = B.file_tp 
    --and A.ingestion_time = B.ingestion_time

到这里:

    SELECT * 
    FROM (select number_id, country, amount, currency from TBL_A)
    PIVOT(MIN(DATETIME) FOR STATUS IN ('claimed', 'bought', 'returned')) 
    AS P (number_id, country, amount, currency, claimed, bought, returned)
) A ON (
    A.number_id = B.number_id, 
    and A.country = B.country, 
    and A.amount = B.amount, 
    and A.currency = B.currency, 

【讨论】:

  • 您好!是的,我最初尝试过这个,但是,当我放置主键并且不包括我需要的主键时,它会弄乱整个订单。它会将所有对应于 file_tp 的值放入声明中,依此类推。
  • 因此,如果我将 P 设为 (number_id, country, amount, currency, claim, buy, returned),那么它会抓取原始表中的所有列,并根据我在无论值如何列出
  • 如果您需要进一步的帮助,您需要向我们提供一些示例数据以及所需结果的外观 - 请明确说明当前查询是如何失败的。
猜你喜欢
  • 1970-01-01
  • 2015-04-02
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 2021-10-16
  • 2011-04-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多