复盘工作2024-12-11
1.练习借助窗口函数实现对表按某字段分组后取每组的最近创建的记录
/* 练习借助窗口函数实现对表按某字段分组后取每组的最近创建的记录 */ /* 举例练习:获取缺陷表里,每个设备的最新创建的缺陷 */ /* 我一开始练习时如下写法是错误的。 select t.*, ROW_NUMBER() OVER((PARTITION BY t.qxzsb) order by t.create_date desc) as rn from pm_yj_qxjl t where rn=1; 错误1:应该是ROW_NUMBER() OVER(PARTITION BY t.qxzsb order by t.create_date desc) as rn,即PARTITION BY t.qxzsb无需再套一层括号 错误2:因为where子句是在窗口函数ROW_NUMBER()执行之前执行的,所以应该再套一层查询 */ select * from (select t.*, ROW_NUMBER() OVER(PARTITION BY t.qxzsb order by t.create_date desc) as rn from pm_yj_qxjl t) where rn = 1;
标签:qxzsb,12,OVER,PARTITION,NUMBER,2024,rn,复盘,ROW From: https://www.cnblogs.com/pingfanliliang/p/18603521