[英]Delete or change records in ETL
我有一张表,我在上面构建了 ETL 服务。 货物记录(到达/离开)去表。 我已经完成了我的表将被删除。 当项目标识符第二次到达数据库时,两条记录都被删除。
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
现在 ETL 服务删除记录(每 30 秒):
label cost time
x3 20 14/5/2020 01:02:00
我使用函数删除它:
with todelete as (
select *, count(*) over (partition by label) as cnt, ROW_NUMBER() over (partition by label order by time DESC) as r_number
from Table1
)
delete from todelete
where cnt >= 2
另一个问题阻碍了。 而当涉及到表时,这仅意味着价格的变化。
变体 1:
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
现在“删除功能”和
我的目标:
label cost time
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.