【问题标题】:Delete or change records in ETL删除或更改 ETL 中的记录
【发布时间】:2020-09-10 12:56:11
【问题描述】:

我有一张表,我在该表上构建了 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

我不知道如何在删除函数中处理这两件事。

【问题讨论】:

  • ROW_NUMBER() over (partition by label order by time DESC) as r_number有什么用?

标签: sql sql-server ssis etl


【解决方案1】:

为了满足您的这两个要求,您应该检查成本变化,如下所示

 ; 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)

【讨论】:

猜你喜欢
  • 2021-04-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多