sql - 删除或更改 ETL 2 中的记录
问题描述
我想跟进我之前 POST 中的一个问题:删除或更改 ETL 中的记录
那里提到的问题已通过以下方式解决:
; with todelete as (
select *,
count(*) over (partition by label) as cnt,
lag(cost) over (partition by label order by time ASC) as lastcost
ROW_NUMBER() over (partition by label order by time ASC) as r_number
from Table1
)
delete from todelete
where cnt > 1 and r_number between 1 and (cnt/2)*2 and cost=ISNULL(lastcost,cost)
,然而,在测试过程中,当这些运动发生在桌子上时,我遇到了一个问题(它们无法阻止)。:
label cost time
x2 29 14/5/2020 01:00:00
x3 20 14/5/2020 01:02:00
x2 29 15/5/2020 03:12:02
对于相同的“标签”,我有两条相同的行(时间戳除外)具有相同的“成本”。而只是上述解决方案将删除这两条记录。我只需要删除旧的。
谢谢你的建议
更新:
我的目标是。
我有记录表:
label cost time
x2 29 14/5/2020 01:00:00
x3 20 14/5/2020 01:02:00
x2 30 15/5/2020 03:12:02
现在我有删除功能:
; with todelete as (
select *,
count(*) over (partition by label) as cnt,
lag(cost) over (partition by label order by time ASC) as lastcost
ROW_NUMBER() over (partition by label order by time ASC) as r_number
from Table1
)
delete from todelete
where cnt > 1 and r_number between 1 and (cnt/2)*2 and cost=ISNULL(lastcost,cost)
我得到我的他们想要一张桌子:
label cost time
x3 20 14/5/2020 01:02:00
x2 30 15/5/2020 03:12:02
但是当原始表如下所示时会出现问题:
label cost time
x2 29 14/5/2020 01:00:00
x3 20 14/5/2020 01:02:00
x2 29 15/5/2020 03:12:02
现在删除功能(上面提到)
我要一张桌子:
label cost time
x3 20 14/5/2020 01:02:00
使用上述删除功能,标签“X2”的两条记录都将被删除,但我只想删除较旧的记录。
解决方案
没人什么?
我试试这个:我无法解决它。在这里,您会看到它会为我删除两条记录(我只想要旧的): https ://rextester.com/TLLQ93275
在这种情况下,它可以正常工作,但如果“x2”具有相同的价格(例如,29),它也会删除两个条目。 https://rextester.com/RHB70490
更新:
我终于设法解决了这个问题。我添加了另一个排名函数并对其进行了适当的调整。
; with todelete as (
select *,
count(*) over (partition by label) as cnt,
lag(cost) over (partition by label order by time ASC) as lastcost
,ROW_NUMBER() over (partition by label order by time ASC) as r_number
,ROW_NUMBER() over (partition by label order by time DESC) as r_number2
,RANK() over (partition by cost order by time asc) as TEST
,Row_NUMBER() over (partition by label order by TIME DESC) as TEST2
from Table1
)
DELETE from todelete
where (cnt > 1 and r_number between 1 and (cnt/2)*2 and cost=ISNULL(lastcost,cost) AND TEST2 !=1) OR (cnt>1 AND TEST2<>1 AND r_number2 != 1)
推荐阅读
- c# - C# 异常处理 - 最后一个 catch 块获取是否会重新抛出异常?
- scala - 过滤 CassandraJoinRDD
- sql - SQL 为每个额外计数创建一个重复行
- html - 如何将使用变换 css 属性转换为顺风 css?
- agora.io - Agora 启动云录制返回 400 且没有错误信息
- python - 具有连续替代符号的值的最大子序列的大小?
- azure - Azure Api 管理 url 重写路径
- jenkins - Jenkins 控制台输出在管道的开始和结束之间没有打印任何内容
- apache-kafka - 找不到任何实现“org.apache.flink.table.factories.DeserializationFormatFactory”的标识符“avro-confluent”的工厂
- input - 使用 put 和 proc 格式在 SAS 中创建新列