我有一个每天需要修剪的数据集。它是从将记录定期写入表的过程中填充的。

我目前有一个简单的查询,可以做到这一点:

DELETE FROM dataTable WHERE entryDate < dateadd(day, -5, GETDATE())


但是问题是该过程不可靠。可能有几天根本没有写入任何数据。

因此,我真正需要的是一个查询,该查询可以回溯5天(可能是非连续的)写入数据的日期,而不是5个日历日。

例如,如果我运行以下查询:

SELECT cast(entryDate  as date) as LogDate
  FROM dataTable
  group by category, cast(entryDate as date)
  order by cast(entryDate as date) desc


结果可能是:

Category    Date
Foo        2015-11-30
Foo        2015-11-29
Foo        2015-11-26
Foo        2015-11-25
Foo        2015-11-21
Foo        2015-11-20  <-- Start Pruning here, not the 25th.
Foo        2015-11-19
Foo        2015-11-18

Bar        2015-11-30
Bar        2015-11-29
Bar        2015-11-28
Bar        2015-11-27
Bar        2015-11-26
Bar        2015-11-25  <-- This one is OK to prune at the 25th.
Bar        2015-11-24
Bar        2015-11-23


我需要查询才能一直删除到20。

最佳答案

您可以使用row_number获取表中有条目的最近5天。然后根据生成的数字删除。

SQL Fiddle

with rownums as (SELECT row_number() over(partition by category order by cast(entryDate as date) desc) as rn
                 ,*
                 FROM dataTable
)
delete from rownums where rn <= 5 --use > 5 for records prior to the last 5 days


如果每天可以有多个条目,请使用dense_rank为行编号。

with rownums as (SELECT dense_rank() over(partition by category order by cast(entryDate as date) desc) as rn
                     ,*
                 FROM dataTable)
delete from rownums where rn > 5;

关于c# - 查询以删除每个组中早于n个有效日期的记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34005749/

10-11 03:31
查看更多