Postgres Case Expression Errors
16:08 22 Jan 2016

I'm attempting to create a PostgreSQL case expression that is based on job posting status. For example:

  • When status is 'Closed', I want the difference in days between the updated_at timestamp and the created_at timestamp.
  • When status is either 'Open' or 'Pending', I want to subtract the created_at timestamp from the current timestamp.

The query I've created so far is listed below:

Select users.first_name || " " || users.last_name,
users.email,
organizations.name,
organizations.vertical,
jobs.name,
jobs.id
(Case
When jobs.status = 'Closed' Then jobs.updated_at - jobs.created_at Else 'Not Closed') END AS days_to_hire,
(Case
When jobs.status IN ('Open,'Pending') Then current_timestamp - jobs.created_at Else 'Closed_or_Deleted') END AS days_open,
FROM organizations
JOIN users on organizations.id = users.organization_id
JOIN jobs on user.id= jobs.user.id

It does not work as I expect, though:

ERROR: syntax error at or near ")"

And I have not been able to figure out why.

sql postgresql case