Short Answer
You have three syntax issues in your query:
- a missing comma
- misplaced closing parentheses in your
CASE statements
- an unterminated string literal in your second
CASE statement
I have noted each below:
Select users.first_name || " " || users.last_name,
users.email,
organizations.name,
organizations.vertical,
jobs.name,
-- Missing comma added below.
jobs.id,
--jobs.id
-- Closing paren moved to after `END` below.
(Case
When jobs.status = 'Closed' Then jobs.updated_at - jobs.created_at Else 'Not Closed' END) AS days_to_hire,
--(Case
--When jobs.status = 'Closed' Then jobs.updated_at - jobs.created_at Else 'Not Closed') END AS days_to_hire,
-- Closing paren moved to after `END` and a closing single quote added
-- to terminate the string literal 'Open' below.
(Case
When jobs.status IN ('Open','Pending') Then current_timestamp - jobs.created_at Else 'Closed_or_Deleted' END) AS days_open,
--(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
Addendum
Also, your query is pretty difficult to read the way you have it formatted. This makes it harder to debug.
Consider an alternate approach that leverages indentation and consistent keyword casing (i.e. lowercase vs. uppercase):
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
Much easier to read and debug I think you will agree.
Personally I do not care for the parentheses around the CASE statements, but that is a minor choice compared to good, consistent use of indentation and keyword casing.