gpt4 book ai didi

sql - 插入旧记录并用新记录替换旧记录

转载 作者:行者123 更新时间:2023-12-04 03:39:07 26 4
gpt4 key购买 nike

我有一个表,它使用 sqoop 获取数据,每天它都会被截断。

开头的这个 tblSqoop 具有这些值:

+----+-------+--------------+---------------+---------+--------+
| id | names | created_date | modified_date | country | number |
+----+-------+--------------+---------------+---------+--------+
| 33 | nick | 1/1/2020 | 1/1/2020 | Dubai | 1234 |
| 45 | ted | 2/7/2020 | 2/7/2020 | Spain | 12345 |
+----+-------+--------------+---------------+---------+--------+

并通过插入到 tblMaxed 中进行解析。

第二天 tblSqoop 有这个数据:

 +----+-------+--------------+---------------+---------+--------+
| id | names | created_date | modified_date | country | number |
+----+-------+--------------+---------------+---------+--------+
| 33 | nick | 1/1/2020 | 12/31/2020 | Dubai | 1234 |
| 45 | ted | 2/7/2020 | 8/19/2020 | Spain | 12345 |
| 45 | ted | 2/7/2020 | 9/12/2020 | Spain | 12345 |
| 45 | ted | 2/7/2020 | 10/11/2020 | Spain | 12346 |
| 45 | ted | 2/7/2020 | 1/1/2021 | Spain | 12345 |
+----+-------+--------------+---------------+---------+--------+

我想要的是在 tblMaxed 中包含最新的信息,例如:

+----+-------+--------------+---------------+---------+--------------------+
| id | names | created_date | modified_date | country | number |status_date|
+----+-------+--------------+---------------+---------+--------+-----------+
| 33 | nick | 1/1/2020 | 12/31/2020 | Dubai | 1234 |12/31/2020 |
| 45 | ted | 2/7/2020 | 10/11/2020 | Spain | 12346 |10/11/2020 |
| 45 | ted | 2/7/2020 | 1/1/2021 | Spain | 12345 |1/1/2021 |
+----+-------+--------------+---------------+---------+--------+-----------+

我正在运行这个:

insert into tblMaxed 
select
id,
names,
created_date,
modified_date,
country,
number,
MAX(modified_date) as status_date
from tblSqoop
group by id,
names,
created_date,
modified_date,
country,
number

但结果我把所有的记录都拿了一遍。对PK的使用有帮助吗?

最佳答案

你能截断表并使用它重新加载 tblMaxed 吗? (解释在代码中)

select 
id,
names,
created_date,
modified_date,
country,
number,
modified_date as status_date
FROM
(select t.*, row_number() OVER (PARTITION BY id,number Order by id,number , modified_date desc) rn from tblSqoop t) rs
where rs.rn=1 -- This will pick up data for MAX modified_date from sqoop table

关于sql - 插入旧记录并用新记录替换旧记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66368499/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com