Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8250643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:57:45+00:00 2026-06-07T23:57:45+00:00

I have two tables that look like this: CREATE TABLE table1 (user_id int, the_date

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T23:57:47+00:00Added an answer on June 7, 2026 at 11:57 pm

    You would need to table-qualify t1.user_id to disambiguate. Plus other adjustments:

    CREATE TABLE foo AS 
    SELECT user_id, (t1.the_date - (t2.the_date - t1.the_date)) AS start_date
    FROM   table1 t1
    JOIN   table2 t2 USING (user_id);
    
    • Subtracting two dates yields integer. Cast was redundant.

    • Don’t omit the AS keyword for column aliases – while it’s generally OK to omit AS for table aliases. The manual:

      You can omit AS, but only if the desired output name does not match
      any PostgreSQL keyword (see Appendix C). For protection against
      possible future keyword additions, it is recommended that you always
      either write AS or double-quote the output name.)

    • Joining tables with a USING clause only keeps one instance of the joining columns(s) (user_id in this case) in the result set and you don’t have to table-qualify it any more.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables in my database that look like this CREATE TABLE IF
I have two tables that look like this: Table: cases id name status case_no
I have two already-existing tables which look (in part) roughly like this: CREATE TABLE
Let's say I have two tables that look like this: TH TH TH TH
I have two tables like this create table A_DUMMY ( TRADE_ID VARCHAR2(16) TRADE_DATA VARCHAR2(500)
I have two tables in my database that look like that: Customer: C_ID city
In my application I have a sqlite database that looks like this: CREATE TABLE
Hi Guys i have two tables which look like this usertable RoleTable ----------------------- ---------------------------
I have two tables that I want to join together. Table1 Year, ID, Theme,
I have two tables that are related via a mapping table: keywords titles I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.