简体   繁体   English

SNOWFLAKE 数据库中关于主键错误的合并语句

[英]Merge statement in SNOWFLAKE database on primary key error

I am kinda confused with the way merge statement has to be written in my scenario to load data from a table into dimension and fact tables.我对在我的场景中必须编写合并语句以将数据从表加载到维度和事实表的方式感到困惑。

Below is the merge statement where I am trying to load data into my DIM product table from the JSON table but I am getting NULL values loaded.下面是合并语句,我试图将数据从 JSON 表加载到我的 DIM 产品表中,但我正在加载 NULL 值。

Am I doing something wrong with the merge and should I not merge on primary key and instead on the fields like cityname我是否在合并时做错了什么,我不应该在主键上进行合并,而是在 cityname 等字段上进行合并

I am having the same issue while trying to load data into the fact table我在尝试将数据加载到事实表中时遇到了同样的问题

Could someone please help?有人可以帮忙吗?

merge into dim_product as a using (select productID, product from jsontable) as b
on b.productID = a.productID
when matched then update a.productID = b.productID
when not matched then insert (productID, product) values (b.productID, b.product));

Below is the Existing Dimension Products table下面是现有维度产品表

在此处输入图片说明

Below is the sample JSON from which I am trying to merge and insert the new record Mango into my DIM table and also populate the PRODUCTID into my fact table下面是示例 JSON,我试图从中合并新记录Mango并将其插入到我的 DIM 表中,并将PRODUCTID填充到我的事实表中

在此处输入图片说明

Below is the fact table下面是事实表

在此处输入图片说明

If we do not have ProductID on the source and set it only in Dim_Product, we should use the business key.如果我们在源上没有 ProductID 并且只在 Dim_Product 中设置它,我们应该使用业务键。 In your case, ProductName is the business key.在您的情况下, ProductName 是业务密钥。 The solution is simple, when you are doing MERGE you should use ProductName as the key instead of ProductID.解决方案很简单,当您进行 MERGE 时,您应该使用 ProductName 而不是 ProductID 作为键。

Your MERGE should look similar to this:您的 MERGE 应该类似于:

merge into dim_product as a using (select ProductName from jsontable) as b
on b.ProductName = a.ProductName
when not matched then insert (ProductName) values (b.ProductName));

If you have more attributes describing the Product, they should be modified inside the MERGE.如果您有更多描述产品的属性,则应在 MERGE 中修改它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM