I have two tables that look like this:
CREATE TABLE table1 (user_id int, the_date date);
CREATE TABLE table2 (user_id int, the_date date, something_else real);
I am writing a query that looks like this
CREATE TABLE foo AS
SELECT t1.user_id
, (t1.the_date - (t2.the_date - t1.the_date)::int) start_date
FROM table1 t1, table2 t2
where t1.user_id=t2.user_id
;
When I run the above query, I get the following error displayed on the psql console:
ERROR: syntax error at or near "$1"
LINE 1: ...the_date - (t2.the_date - t1.the_date)::int) $1 ...
^
The second column in the query result is to show a date which is N days BEFORE
the date in table1, where N is the difference (in days) between the date in table2 and table1.
Note: table2
will always have has later dates than the dates in table1
.
How can I perform this date calculation and store the result as a new column alias in my query?
I am using PG 8.4.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…