postgresql 如何在postgres中获取具有最大时间戳的行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14653257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the row with max timestamp in postgres?
提问by Gob00st
I need to get a row of information with max timestamp in postgresql. Below is a demo for this question:
我需要在 postgresql 中获取一行具有最大时间戳的信息。下面是这个问题的演示:
drop table Mytable cascade
create table MyTable (usr char(1), event_dt timestamp without time zone);
insert into mytable values ('A','01-JAN-2009 11:10:11');
insert into mytable values ('A','02-JAN-2009 11:10:22');
insert into mytable values ('B','02-JAN-2009 01:01:59' );
insert into mytable values ('C', '31-DEC-2008 02:02:02');
insert into mytable values ('D', '31-DEC-2008 03:03:03');
If I do
如果我做
select max(event_dt) from (
select usr,event_dt from mytable where usr= 'A') as foo
It is sort of what I need but it only returns the event_dt "2009-01-02 11:10:22"
这是我需要的,但它只返回 event_dt "2009-01-02 11:10:22"
Where I want the usr as well sa event_dt from that row. How do I do it?
我想要来自该行的 usr 以及 sa event_dt 。我该怎么做?
回答by Najzero
I would simply go for...
我只会去...
SELECT usr, event_dt FROM mytable ORDER BY event_dt DESC LIMIT 1