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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:11:36+00:00 2026-06-03T20:11:36+00:00

I am trying to write a query that takes both the max and min

  • 0

I am trying to write a query that takes both the max and min values from a table group by id. Basically, I have a table that adds renewals for new annual amounts off an existing contract_id as such:

customer_id | contract_id | start_date | end_date   | revenue
6           | 125         | 2012-01-01 | 2012-12-31 | 10,000
6           | 126         | 2012-01-01 | 2013-12-31 | 5,000
6           | 125         | 2013-01-01 | 2013-12-31 | 12,000
6           | 126         | 2014-01-01 | 2015-12-31 | 8,000

What I want to take the minimum start_date, the maximum end_date, and the corresponding revenue for that end_date, grouped by contract_id for a given customer_id.

So, the query would ideally return:

customer_id | contract_id | start_date | end_date   | revenue
6           | 125         | 2012-01-01 | 2013-12-31 | 12,000
6           | 126         | 2012-01-01 | 2015-12-31 | 8,000

I can get the max stuff, but I’m having trouble pulling in both MAX and MIN in the same query. Any help would be greatly appreciated. Thanks!

  • 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-03T20:11:38+00:00Added an answer on June 3, 2026 at 8:11 pm

    You should try this Query also see the screenshot for verification (see image 1)

    SELECT r.`contract_id`,r.`start_date`,
    (SELECT sub_r.`end_date` FROM `table` sub_r where sub_r.`contract_id` = r.`contract_id` 
    ORDER BY sub_r.`revenue` DESC limit 1 )
    AS `end_date`,
    (SELECT sub_r.`revenue` FROM `table` sub_r where sub_r.`contract_id` = r.`contract_id` 
    ORDER BY sub_r.`revenue` DESC limit 1 )
    AS `revenue` 
    FROM `table` r GROUP BY r.`contract_id`;
    

    Image 1

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a query that connects 3 tables. The first table
I am trying to write a query that joins another table, but in this
I have a SQL query that I'm trying to write, but I'm not quite
I have a sqlite query that I'm trying to write. I have two tables:
I am trying to write a stored procedure that takes a table name as
I'm trying to write a query that selects from four tables campaignSentParent csp campaignSentEmail
I'm trying to write a query that takes a criterion and returns a count
I'm trying to write a function that will remove a query argument from a
I'm trying to write a query that gives me a percentage (i.e. something like
I'm trying to write a GQL query that returns N random records of a

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.