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 8494769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:23:22+00:00 2026-06-10T23:23:22+00:00

I have the following query which works great in Postgres 9.1: SELECT users.id, GREATEST(

  • 0

I have the following query which works great in Postgres 9.1:

SELECT users.id, GREATEST(
 COALESCE(MAX(messages.created_at), '2012-07-25 16:05:41.870117'),
 COALESCE(MAX(phone_calls.created_at), '2012-07-25 16:05:41.870117')
) AS latest_interaction
FROM users LEFT JOIN messages ON users.id = messages.user_id
LEFT JOIN phone_calls ON users.id = phone_calls.user_id
GROUP BY users.id
ORDER BY latest_interaction DESC
LIMIT 5;

But what I want to do is something like this:

SELECT users.id, GREATEST(
 COALESCE(MAX(messages.created_at), '2012-07-25 16:05:41.870117'),
 COALESCE(MAX(phone_calls.created_at), '2012-07-25 16:05:41.870117')
) AS latest_interaction
FROM users LEFT JOIN messages ON users.id = messages.user_id
LEFT JOIN phone_calls ON users.id = phone_calls.user_id
GROUP BY users.id
ORDER BY
  CASE WHEN(
    latest_interaction > '2012-09-05 16:05:41.870117')
  THEN 0
  WHEN(latest_interaction > '2012-09-04 16:05:41.870117')
  THEN 2
  WHEN(latest_interaction > '2012-09-04 16:05:41.870117')
  THEN 3
  ELSE 4
  END
LIMIT 5;

And I get the following error:
ERROR: column “latest_interaction” does not exist

It seems like I cannot use the alias for the aggregate latest_interaction in the order by clause with a CASE statement.

Are there any workarounds for this?

  • 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-10T23:23:24+00:00Added an answer on June 10, 2026 at 11:23 pm

    Try to wrap it as a subquery:

    SELECT * 
    FROM 
    (
        SELECT users.id, 
            GREATEST(
                 COALESCE(MAX(messages.created_at), '2012-07-25 16:05:41.870117'),
                 COALESCE(MAX(phone_calls.created_at), '2012-07-25 16:05:41.870117')
            ) AS latest_interaction
            FROM users LEFT JOIN messages ON users.id = messages.user_id
            LEFT JOIN phone_calls ON users.id = phone_calls.user_id
            GROUP BY users.id
    ) Sub
    ORDER BY
      CASE WHEN(
        latest_interaction > '2012-09-05 16:05:41.870117')
      THEN 0
      WHEN(latest_interaction > '2012-09-04 16:05:41.870117')
      THEN 2
      WHEN(latest_interaction > '2012-09-04 16:05:41.870117')
      THEN 3
      ELSE 4
      END
    LIMIT 5;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query which works: SELECT ?page ?lat ?long (bif:st_distance(?geo, bif:st_point(42.883, -72.8981)))
I have the following which works in SQL Query Analyzer . select oh.* from
I have the following query: SELECT created_at::DATE, count (*) FROM messages WHERE city =
I have the following query I use and it works great: SELECT * FROM
I have the following Query which works great! The only thing is that I
I have the following function in my iPhone project which works great...unless the query
I have the following query so far, which works perfectly to rank the books
I have the following query which have 1000 rows select staffdiscountstartdate,datediff(day,groupstartdate,staffdiscountstartdate), EmployeeID from tblEmployees
I have the following code which works great:- $('.ajax a').click(function() { getpageajax(this.href); return false;
I have the following query which works fine in SSMS. Im using LinqPad (

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.