对于返回的每一行,我想将它的时间戳列与SELECT中的第一行进行比较,并获得以天为单位的日期差异。我怎么能这样做?
List(1, 2, 3, 5).patch(3, List(4), 0)
期望的输出
SELECT date
FROM table
ORDER BY DATE ASC
答案 0 :(得分:1)
由freenode上的#postgresql提供:
% dropdb testdb; createdb testdb; psql -X testdb
psql (9.4.4)
Type "help" for help.
testdb=# create table t as select 0 id, '2015-05-02'::timestamp date;
SELECT 1
testdb=# insert into t select 1, '2015-05-05';
INSERT 0 1
testdb=# insert into t select 2, '2015-05-22';
INSERT 0 1
testdb=# select id, date, date - min(date) over () from t;
id | date | ?column?
----+---------------------+----------
0 | 2015-05-02 00:00:00 | 00:00:00
1 | 2015-05-05 00:00:00 | 3 days
2 | 2015-05-22 00:00:00 | 20 days
(3 rows)
答案 1 :(得分:0)
这可能有点晚,但您可以使用first_value
窗口功能。见http://www.postgresql.org/docs/9.1/static/functions-window.html
select id, date, date - first_value(date) over ()
from Table1;