SQL クエリを作成する効率的な方法を探しています。
列 ( id
、fname
、lname
、accountid
、creation_date
)を持つテーブルがあり、そのテーブル内で同じ、、およびに基づく最新の日付をupdate_date
持つレコードを探す必要があります ( null の可能性があることに注意してください)。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')