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!
You should try this Query also see the screenshot for verification (see image 1)