sql - Keep only most recent rows by date
问题描述
Having this table
CREATE TABLE BOOKINGS
([RES_ID] varchar(4), [ATTENDANCE_DATE] datetime, [AUTOID] int);
INSERT INTO BOOKINGS
([RES_ID], [ATTENDANCE_DATE], [AUTOID])
VALUES
('A001', '2018-01-01 00:00:00', 1),
('A002', '2018-01-01 00:00:00', 2),
('A003', '2018-01-01 00:00:00', 3),
('A001', '2018-01-02 00:00:00', 4),
('A002', '2018-01-02 00:00:00', 5),
('A003', '2018-01-02 00:00:00', 6),
('A002', '2018-01-03 00:00:00', 7),
('A003', '2018-01-03 00:00:00', 8);
I would like to remove all rows with id='A001'
since at the most recent date, there is no reservation (i.e. was canceled).
I have tried this:
with cte as
(
select *,
row_number() over(partition by [res_id]
order by [ATTENDANCE_DATE] desc) rn
from BOOKINGS
)
DELETE FROM cte where rn > 1;
But this keeps the most recent line for 'A001'
(id=4) and I don't want that.
Expected output is:
A002 2018-01-03 00:00:00.000 7
A003 2018-01-03 00:00:00.000 8
解决方案
一种方法是将每个resid
的最近日期与总体上的最近日期进行比较。您可以使用窗口函数执行此操作:
with todelete as (
select b.*,
max(attendance_date) over (partition by res_id) as max_ad_resid,
max(attendance_date) over () as max_ad
from bookings b
)
delete from todelete
where max_ad_resid < max_ad;
推荐阅读
- mysql - pt-online-schema-change 中断 AWS DMS 复制
- powershell - Rename-Item : Powershell 中的源路径和目标路径必须不同错误
- r - 如何在长弹性表的标题之前的每一页上打印短标题(rmarkdown,字输出)
- .net-core - 不同框架之间的私有 NuGet 兼容性
- python - 为什么我的代码要比较两种不同排序算法的运行时间多次显示相同的数字?
- ssl - Roundcube SSL 连接 IMAP 错误:登录失败
- android - com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:应为 BEGIN_OBJECT 但为 BEGIN_ARRAY
- c - 将新项目添加到C中的结构数组
- asp.net-mvc - 图 api 响应失败案例
- mysql - Liquibase 使用变量来设置变更集作者