删除或更改 ETL 中的记录
Delete or change records in ETL
我有一个 table,我在上面构建了一个 ETL 服务。货物记录(到达/离开)去table。我已经完成了,我的 table 将被删除。当项目标识符第二次到达数据库时,两条记录都被删除。
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
另一个问题阻碍了。而到了table,也只是价格的变化。
变体 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)
我有一个 table,我在上面构建了一个 ETL 服务。货物记录(到达/离开)去table。我已经完成了,我的 table 将被删除。当项目标识符第二次到达数据库时,两条记录都被删除。
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
另一个问题阻碍了。而到了table,也只是价格的变化。
变体 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)