我正在寻找一种编写 SQL 查询的高性能方法。
我有一个包含列 ( id
, fname
, lname
, accountid
, creation_date
, ) 的表,update_date
我必须在该表中查找具有相同fname
,并且最近日期基于的记录(注意可以为空)lname
accountid
greatest(max(creation_date),max(update_date))
update_date
我希望我需要使用分析函数
我有这些情况:
(id,fname,lname,accountid,creation_date,update_date)
(1,'a','a','2','07/01/2010 10:59:43','07/01/2010 10:59:43')
(2,'a','a','2','07/01/2010 10:59:43','07/01/2010 10:59:43')
(3,'a','a','2','07/01/2010 10:59:43','07/01/2010 10:59:43')
我想选择最后插入的:这条记录 (3,'a','a','2','07/01/2010 10:59:43','07/01/2010 10:59:43' )
(id,fname,lname,accountid,creation_date,update_date)
(3,'b','a','2','07/01/2009 10:59:43','07/01/2010 10:59:43')
(4,'b','a','2','07/01/2011 10:59:43',null)
(5,'b','a','2','07/01/2009 10:59:43','07/01/2009 10:59:43')
我想在两列 (creation_date,update_date) 上选择最新的一个,即 (4,'b','a','2','07/01/201110:59:43',null)
(id,fname,lname,accountid,creation_date,update_date)
(6,'c','g','4','07/01/2010 10:59:43',null)
(7,'c','g','4','07/01/2011 10:59:43',null)
(8,'c','g','4','07/01/2009 10:59:43',null)
我想在两列 (creation_date,update_date) 上选择最新的一个,即 (7,'c','g','4','07/01/2011 10:59:43',null)
(id,fname,lname,accountid,creation_date,update_date)
(9,'k','t','2','07/01/2009 10:59:43','07/01/2012 10:59:43')
(10,'k','t','2','07/01/2011 10:59:43',null)
(11,'k','t','2','07/01/2009 10:59:43','07/01/2009 10:59:43')
我想在两列 (creation_date,update_date) 上选择最新的一个,即 (9,'k','t','2','07/01/2009 10:59:43','07/01 /2012 10:59:43')