与 ROW_NUMBER 合并 - Tbl1 作为主要 table | tbl2 合并入 |仅合并 tbl1 中的最新条目
MERGE with ROW_NUMBER - Tbl1 as main table | tbl2 merging in | merge on latest entry in tbl1 only
我想将两个 table 合并在一起(tbl1 和 tbl2)
Tbl1 是包含所有产品和价格变化历史的主 table。
Tbl2 仅包含当天发生更改的产品
我想比较 tbl2 和 tbl1,当 tbl1 上产品的最新条目与 tbl2 上列出的价格不同时,它应该在 tbl1 上创建一个新行条目。如果数据匹配,则无需更改。
我理解 Merge into 和 Row_Number 但这似乎适用于我将 tbl1 合并到 tbl2 的实例,而不是其他方式
例如
MERGE INTO tbl1 USING tbl2
ON CONCAT(tbl1.product, tbl1.price) = CONCAT(tbl2.product, tbl2.price)
WHEN NOT MATCHED THEN
INSERT (CHANGE_ID, product, price, UPDATED_AT, MODIFIED_DATETIME)
values (CONCAT(current_time, product), product, price, UPDATED_AT, MODIFIED_DATETIME);
然而,此代码将搜索所有条目。我希望做的是以某种方式使用 ROW_NUMBER() 如下所示,但似乎只能在 table 上执行 ROW_NUMBER 合并
MERGE INTO
(select * FROM (SELECT ROW_NUMBER() OVER (PARTITION BY product ORDER BY MODIFIED_DATETIME DESC) AS ROWCOUNT, product, price FROM tbl1) where ROWCOUNT = 1) tblnew
USING tbl2
ON CONCAT(tblnew.product, tblnew.price) = CONCAT(tbl2.product, tbl2.price)
WHEN NOT MATCHED THEN
INSERT (CHANGE_ID, product, price, UPDATED_AT, MODIFIED_DATETIME)
values (CONCAT(current_time, product), product, price, UPDATED_AT, MODIFIED_DATETIME);
我什至尝试先创建一个 select 子句并将其设置为 table 然后在合并中使用它。我认为这更有可能是正确的前进方式,我似乎无法将 table 用于合并
Select * from(select * FROM (SELECT ROW_NUMBER() OVER (PARTITION BY product ORDER BY MODIFIED_DATETIME DESC) AS ROWCOUNT, product, price FROM tbl1) where ROWCOUNT = 1) as tblnew
MERGE INTO tblnew USING tbl2
ON CONCAT(tblnew.product, tblnew.price) = CONCAT(tbl2.product, tbl2.price)
WHEN NOT MATCHED THEN
INSERT (CHANGE_ID, product, price, UPDATED_AT, MODIFIED_DATETIME)
values (CONCAT(current_time, product), product, price, UPDATED_AT, MODIFIED_DATETIME);
使用 INSERT INTO 会更好吗?我想使用 MERGE INTO 的部分原因是为任何匹配增加 modified_datetime,这样我就可以识别尚未检查的产品。
编辑以添加源示例
Tbl1
Change_ID
Product
Price
Updated_At
Modified_dateTime
apple02/05/2022
apple
1.30
02/05/2022
02/05/2022 11:11:01
orange02/05/2022
orange
0.99
02/05/2022
02/05/2022 11:11:02
pear02/05/2022
pear
1.50
02/05/2022
02/05/2022 11:11:03
pear03/05/2022
pear
1.60
03/05/2022
03/05/2022 17:10:00
apple03/05/2022
apple
1.40
03/05/2022
03/05/2022 17:10:01
Tbl2
Product
Price
Updated_At
apple
1.30
04/05/2022
orange
0.99
04/05/2022
pear
1.60
04/05/2022
因此,我希望苹果在其上添加一个与上次持有的价格不同的新行。
合并后的 Tbl1
Change_ID
Product
Price
Updated_At
Modified_dateTime
apple02/05/2022
apple
1.30
02/05/2022
02/05/2022 11:11:01
orange02/05/2022
orange
0.99
02/05/2022
02/05/2022 11:11:02
pear02/05/2022
pear
1.50
02/05/2022
02/05/2022 11:11:03
pear03/05/2022
pear
1.60
03/05/2022
03/05/2022 17:10:00
apple03/05/2022
apple
1.40
03/05/2022
03/05/2022 17:10:01
apple04/05/2022
apple
1.30
04/05/2022
04/05/2022 16:00:00
以下查询将根据 updated_dt
给出来自 tbl1 的最新条目
select * from tbl1 qualify row_number()
over (partition by product order by updated_at desc)=1
将上述查询与 tbl2 中的数据结合起来,根据条件“当 tbl1 上的产品最新条目的价格与 tbl2 上列出的价格不同时”获取数据 -
select distinct a.* from
tbl2 a,
(select * from tbl1 qualify row_number()
over (partition by product order by updated_at desc)=1) b
where a.product = b.product
and a.price != b.price -- different price
要在 tbl2 中包含 tbl1 中缺少的其他行,UNION 可以用作 -
select distinct a.* from
tbl2 a,
(select * from tbl1 qualify row_number()
over (partition by product order by updated_at desc)=1) b
where a.product = b.product
and a.price != b.price
UNION ALL -- Add any rows in tbl2 that are new and not present in tbl1
select * from Tbl2 where product not in (select product from tbl1);
使用上面的 select INSERT 可以执行为 -
insert into tbl1
select
product||to_char(Updated_At,'dd/mm/yyyy'),product,price,updated_at,current_timestamp()
from
(
select distinct a.* from
tbl2 a,
(select * from tbl1 qualify row_number()
over (partition by product order by updated_at desc)=1) b
where a.product = b.product
and a.price != b.price
UNION ALL
select * from Tbl2 where product not in (select product from tbl1)
);
我想将两个 table 合并在一起(tbl1 和 tbl2)
Tbl1 是包含所有产品和价格变化历史的主 table。
Tbl2 仅包含当天发生更改的产品
我想比较 tbl2 和 tbl1,当 tbl1 上产品的最新条目与 tbl2 上列出的价格不同时,它应该在 tbl1 上创建一个新行条目。如果数据匹配,则无需更改。
我理解 Merge into 和 Row_Number 但这似乎适用于我将 tbl1 合并到 tbl2 的实例,而不是其他方式
例如
MERGE INTO tbl1 USING tbl2
ON CONCAT(tbl1.product, tbl1.price) = CONCAT(tbl2.product, tbl2.price)
WHEN NOT MATCHED THEN
INSERT (CHANGE_ID, product, price, UPDATED_AT, MODIFIED_DATETIME)
values (CONCAT(current_time, product), product, price, UPDATED_AT, MODIFIED_DATETIME);
然而,此代码将搜索所有条目。我希望做的是以某种方式使用 ROW_NUMBER() 如下所示,但似乎只能在 table 上执行 ROW_NUMBER 合并
MERGE INTO
(select * FROM (SELECT ROW_NUMBER() OVER (PARTITION BY product ORDER BY MODIFIED_DATETIME DESC) AS ROWCOUNT, product, price FROM tbl1) where ROWCOUNT = 1) tblnew
USING tbl2
ON CONCAT(tblnew.product, tblnew.price) = CONCAT(tbl2.product, tbl2.price)
WHEN NOT MATCHED THEN
INSERT (CHANGE_ID, product, price, UPDATED_AT, MODIFIED_DATETIME)
values (CONCAT(current_time, product), product, price, UPDATED_AT, MODIFIED_DATETIME);
我什至尝试先创建一个 select 子句并将其设置为 table 然后在合并中使用它。我认为这更有可能是正确的前进方式,我似乎无法将 table 用于合并
Select * from(select * FROM (SELECT ROW_NUMBER() OVER (PARTITION BY product ORDER BY MODIFIED_DATETIME DESC) AS ROWCOUNT, product, price FROM tbl1) where ROWCOUNT = 1) as tblnew
MERGE INTO tblnew USING tbl2
ON CONCAT(tblnew.product, tblnew.price) = CONCAT(tbl2.product, tbl2.price)
WHEN NOT MATCHED THEN
INSERT (CHANGE_ID, product, price, UPDATED_AT, MODIFIED_DATETIME)
values (CONCAT(current_time, product), product, price, UPDATED_AT, MODIFIED_DATETIME);
使用 INSERT INTO 会更好吗?我想使用 MERGE INTO 的部分原因是为任何匹配增加 modified_datetime,这样我就可以识别尚未检查的产品。
编辑以添加源示例
Tbl1
Change_ID | Product | Price | Updated_At | Modified_dateTime |
---|---|---|---|---|
apple02/05/2022 | apple | 1.30 | 02/05/2022 | 02/05/2022 11:11:01 |
orange02/05/2022 | orange | 0.99 | 02/05/2022 | 02/05/2022 11:11:02 |
pear02/05/2022 | pear | 1.50 | 02/05/2022 | 02/05/2022 11:11:03 |
pear03/05/2022 | pear | 1.60 | 03/05/2022 | 03/05/2022 17:10:00 |
apple03/05/2022 | apple | 1.40 | 03/05/2022 | 03/05/2022 17:10:01 |
Tbl2
Product | Price | Updated_At |
---|---|---|
apple | 1.30 | 04/05/2022 |
orange | 0.99 | 04/05/2022 |
pear | 1.60 | 04/05/2022 |
因此,我希望苹果在其上添加一个与上次持有的价格不同的新行。
合并后的 Tbl1
Change_ID | Product | Price | Updated_At | Modified_dateTime |
---|---|---|---|---|
apple02/05/2022 | apple | 1.30 | 02/05/2022 | 02/05/2022 11:11:01 |
orange02/05/2022 | orange | 0.99 | 02/05/2022 | 02/05/2022 11:11:02 |
pear02/05/2022 | pear | 1.50 | 02/05/2022 | 02/05/2022 11:11:03 |
pear03/05/2022 | pear | 1.60 | 03/05/2022 | 03/05/2022 17:10:00 |
apple03/05/2022 | apple | 1.40 | 03/05/2022 | 03/05/2022 17:10:01 |
apple04/05/2022 | apple | 1.30 | 04/05/2022 | 04/05/2022 16:00:00 |
以下查询将根据 updated_dt
给出来自 tbl1 的最新条目select * from tbl1 qualify row_number()
over (partition by product order by updated_at desc)=1
将上述查询与 tbl2 中的数据结合起来,根据条件“当 tbl1 上的产品最新条目的价格与 tbl2 上列出的价格不同时”获取数据 -
select distinct a.* from
tbl2 a,
(select * from tbl1 qualify row_number()
over (partition by product order by updated_at desc)=1) b
where a.product = b.product
and a.price != b.price -- different price
要在 tbl2 中包含 tbl1 中缺少的其他行,UNION 可以用作 -
select distinct a.* from
tbl2 a,
(select * from tbl1 qualify row_number()
over (partition by product order by updated_at desc)=1) b
where a.product = b.product
and a.price != b.price
UNION ALL -- Add any rows in tbl2 that are new and not present in tbl1
select * from Tbl2 where product not in (select product from tbl1);
使用上面的 select INSERT 可以执行为 -
insert into tbl1
select
product||to_char(Updated_At,'dd/mm/yyyy'),product,price,updated_at,current_timestamp()
from
(
select distinct a.* from
tbl2 a,
(select * from tbl1 qualify row_number()
over (partition by product order by updated_at desc)=1) b
where a.product = b.product
and a.price != b.price
UNION ALL
select * from Tbl2 where product not in (select product from tbl1)
);