我需要在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');
如果我这样做
select max(event_dt) from (
select usr,event_dt from mytable where usr= 'A') as foo
这是我需要的,但它只返回event_dt “2009-01-02 11:10:22”
我想要usr以及来自该行的event_dt。我该怎么做?
答案 0 :(得分:21)
我只想去......
SELECT usr, event_dt FROM mytable ORDER BY event_dt DESC LIMIT 1