Solved

mysql:  query to find the max within each grouping

Posted on 2013-11-11
4
245 Views
Last Modified: 2013-11-11
i have the following table:    tblOrders   ORD   (primary key is OrderID)
with the following fields
OrderID
CustID
EndDate

I want a query that returns the above three fields WHERE the ORD records have the max  of each customer's EndDates to be
in a given range   (the goal is to evaluate renewal reminder lists)

here's an example of 4 order records (abstracted for discussion)
OrderID CustID EndDate
1                  100    2011
2                  100    2012
3                  200    2012
4                  200    2013

something like:
SELECT OrderID, CustID, EndDate FROM ORD
           WHERE  ( MAX(EndDate) withinEach custid )     <= 2012

(and the desired result would be OrderID=2 record)

i'm not sure if GROUP BY(MAX) is the right strategy, i don't think so, since i can't find how to group by CustID and then find a different field's max(EndDate)

thanks for any help!
0
Comment
Question by:willsherwood
  • 2
  • 2
4 Comments
 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 500 total points
ID: 39640577
SELECT t1.OrderID, t1.CustID, t1.EndDate
  FROM tblOrders AS t1
 INNER JOIN (SELECT CustID,MAX(EnDDate) AS Max_EndDate
                      FROM tblOrders
                    GROUP BY CustID) AS t2
  ON t1.CustID = t2.CustID AND t1.EndDate = t2.Max_EndDate
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 39640583
SELECT t1.OrderID, t1.CustID, t1.EndDate
  FROM tblOrders AS t1
 WHERE t1.EndDate =  (SELECT MAX(t2.EndDate)
                                      FROM tblOrders AS t2
                                    WHERE t1.CustID = t2.CustID)
0
 

Author Closing Comment

by:willsherwood
ID: 39640597
appreciate the quick reply.  MANY thanks!
love the nested SELECT approach.
0
 

Author Comment

by:willsherwood
ID: 39640602
follow up:
how to extend your solution to do the nested inner select in your second example,
to use its value twice if i need the EndDate to be in a range?

date1 < MaxEndDate < date2
i.e.,   date1 < (sub select...)   AND  date2 > (sub select...)

without repeating the inner select (it's not a huge DB and this is done infrequently, so it's not earth shattering to repeat it)

i'm not sure where the "external" variable "2012" (in my original example) comes into play
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now