2

I'm attempting to create a Postgres case statement 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.

Any help would be greatly appreciated.

1
  • Welcome to SO! Be sure to indicate the exact syntax error(s) you were getting. I included the first error I saw in testing your query, but you should include specific error messages in your questions for clarity. Commented Jan 22, 2016 at 19:54

1 Answer 1

2

Short Answer

You have three syntax issues in your query:

  1. a missing comma
  2. misplaced closing parentheses in your CASE statements
  3. 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.

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

1 Comment

@13ISPaulGeorge: If this answered your question satisfactorily, please mark it as accepted. Thanks!

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.