0

I have the following table in postgresql (table1):

timestamp1  timestamp without time zone NOT NULL,
variable1 integer,
timestamp2  timestamp without time zone NOT NULL

I want calculate the timestamp2.

Note that variable 1 is of type integer in table 1, but in practice is a time variable defined in hours

The timestamp2 is defined by difference of timestamp1 and variable1 (timestamp2= timestamp1 – variable1)

For example,

2013-02-06 07:00:00  -  5 = 2013-02-06 02:00:00
2013-02-06 09:00:00  - 12 = 2013-02-05 21:00:00 
2013-02-06 12:00:00  - 4.5  = 2013-02-06 07:30:00           

How to do this calculation (of timestamp2) in postgresql?

2 Answers 2

2
select timestamp1 - interval '1 hour' * variable1
from table1
Sign up to request clarification or add additional context in comments.

Comments

1

Postgres understands the number as time:

  • 5 -> 05:00:00
  • 12 -> 12:00:00
  • 4.5 -> 04:30:00

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.