简体   繁体   中英

PostgreSQL get column with date + interval

If I have table with a two columns:

  • date (timestamp)
  • milliseconds (int)

How could I write a query that would return the two columns and a third column that represents a sum of the first two as a timestamp.

Like this:

            date            | milliseconds  |              sum          
----------------------------+---------------+----------------------------
 2014-12-10 17:43:47.554989 |         11882 | 2014-12-10 17:43:59.436989

Thanks!

I think you answer is as follows:

# select date, milliseconds, date+milliseconds*interval '1 milliseconds' as sum from temp;

             date            | milliseconds |            sum
 ----------------------------+--------------+----------------------------
  2014-12-10 17:43:47.554989 |        11882 | 2014-12-10 17:43:59.436989
 (1 row)

I created a temp table with your timestamp field called date, and your int field called milliseconds. Then ran the above select off the table with your values in it. Hope this helps.

只需将其添加为间隔即可:

select stamp + (ms / 1000) * '1 second'::interval

您正在寻找的价值是

 date + milliseconds * INTERVAL '1 MILLISECOND';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM