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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:45:46+00:00 2026-06-07T13:45:46+00:00

I am trying to create select statement in procedure. Here is my code: SELECT

  • 0

I am trying to create select statement in procedure.

Here is my code:

SELECT u.id, max(a.time), max(b.time)
FROM buy a, sell b, users u
WHERE a.user_id = u.id AND b.user_id = u.id
GROUP BY u.id;

I need to take the last date between max(a.time) and max(b.time).

Thanks in advance

  • 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-07T13:45:47+00:00Added an answer on June 7, 2026 at 1:45 pm
    SELECT u.id, max(case when a.time > b.time then a.time else b.time end)
    FROM buy a, sell b, users u
    WHERE a.user_id = u.id AND b.user_id = u.id
    GROUP BY u.id;
    

    If your DB supports aliased queries, try this which would be much more efficient:

    SELECT u.id, case when a_max > b_max then a_max else b_max end
    FROM (SELECT u.id, max(a.time) as a_max, max(b.time) as b_max
        FROM buy a, sell b, users u
        WHERE a.user_id = u.id AND b.user_id = u.id
        GROUP BY u.id) x;
    

    The inner query could use indexes if they existed to get the max for each column quickly.


    While we’re at it, let’s re-code it using the modern join syntax:

    SELECT u.id, max(case when a.time > b.time then a.time else b.time end)
    FROM buy a
    JOIN sell b ON b.user_id = u.id
    JOIN users u ON a.user_id = u.id
    GROUP BY u.id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a statement as follows: SELECT * FROM table WHERE
I am trying to call a stored procedure from a select statement but it
I am trying to create an SQL select statement in PL/pgSQL procedure. It was
I am trying to create a select list starting from the current date of
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
I'm trying to use the Predicate from Albahari to create a TSQL statement like:
I'm trying to create a simple stored procedure with an if else statement in
I'm trying to create a stored procedure that retrieves data from 3 tables on
I am trying to write a simple stored procedure that does a select statement
Im trying to create a select command to query the database I have and

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.