0

I have this query to update another table but getting error.

With CTE AS
(Select employer_id, employee_id, start_date, end_date, hourly_rate, end_date - start_date AS time_worked
 FROM payroll_timesheet)
INSERT INTO payroll_timelog 
SELECT * FROM CTE
WHERE employee_id = 1;

I get this error:

ERROR:  column "employee_id" is of type integer but expression is of type timestamp with time zone
LINE 5: SELECT employer_id, employee_id, start_date, end_date, hourl...
                                         ^
HINT:  You will need to rewrite or cast the expression.

It seems to be mapping employee_id with start_date.

Or is there a syntax error in the query?

0

1 Answer 1

3

Your current error is an excellent reason for why you should always explicitly list out the column names of the target table in an insert. You should have used something like this:

WITH cte AS (
    SELECT employer_id, employee_id, start_date, end_date, hourly_rate,
           end_date - start_date AS time_worked
    FROM payroll_timesheet
)

INSERT INTO payroll_timelog (employer_id, employee_id, start_date, end_date, hourly_rate,
                             time_worked)
SELECT employer_id, employee_id, start_date, end_date, hourly_rate, time_worked
FROM cte
WHERE employee_id = 1;

You might have to change the column names of the target payroll_timelog table to match. Your current error is happening because Postgres is using the default column order, in the absence of your specifying the column order you want.

Sign up to request clarification or add additional context in comments.

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.