|
Hi, try the following: With x as (Select Name, Year, Make, Date, Sym, Date concat Sym as Combined from MyTable), y as (Select Name, Year, Make, Max(Combined) as MaxCombined from x group by Name, Year, Make) select x.Name, x.Year, x.Make, x.Date, x.Sym from x inner join y on x.Name = y.Name and x.Year = y.Year and x.Make = y.Make and Combined = MaxCombined In the example above, Date and Sym are concatenated together to built a single field. If one or both of the fields are not Char you have to convert them into CHAR. In an second CTE the maximum value for Name, Year, Make is determined. In the final select statement both CTEs are joined over the keys and the combined field, to return only the rows with the maximum values. Birgitta -----Original Message----- From: midrange-l-bounces@xxxxxxxxxxxx [mailto:midrange-l-bounces@xxxxxxxxxxxx] On Behalf Of John Candidi Sent: Monday, December 11, 2006 15:31 To: 'Midrange Systems Technical Discussion' Subject: RE: Need help with SQL query SELECT A.A8FYCD, A.A8HFCD, A.A8E6TX, max(A.A8IONB)max_edition_date, B.MAXSYMBOL FROM Library.File AS A INNER JOIN (SELECT A8FYCD, A8HFCD, A8E6TX, MAX(A8ALIFTE) AS MAXSYMBOL, A8IONB FROM Library.file GROUP BY A8FYCD, A8HFCD,A8E6TX,A8IONB) AS B ON A.A8FYCD = B.A8FYCD AND A.A8HFCD = B.A8HFCD AND A.A8E6TX = B.A8E6TX AND A.A8IONB = B.A8IONB GROUP BY A.A8FYCD, A.A8HFCD,A.A8E6TX,A.A8IONB,B.MAXSYMBOL ORDER BY A.A8FYCD asc, A.A8HFCD,A.A8E6TX,A.A8IONB,B.MAXSYMBOL Returns from the table Name Year make Date Sym Acur 00 3.2TL blank 18 Acur 00 3.2TL 1000 15 The above gets me 2 lines like the sample output above. I just want to return that 2nd line from above which is the highest date (1000) and the highest symbol associated with that date. Below is sample of the db Name Year make Date Sym Acur 00 3.2TL blank 18 Acur 00 3.2TL blank 17 Acur 00 3.2TL 1000 15 Acur 00 3.2Tl blank 14 -----Original Message----- From: midrange-l-bounces@xxxxxxxxxxxx [mailto:midrange-l-bounces@xxxxxxxxxxxx] On Behalf Of Mark Adkins Sent: Friday, December 08, 2006 11:13 AM To: midrange-l@xxxxxxxxxxxx Subject: RE: Need help with SQL query Here is another solution. It is pretty elegant IMO: SELECT A.FNAME, A.LNAME, MAX(A.AGE), B.MAXSALARY FROM TABLE AS A INNER JOIN (SELECT FNAME, LNAME, MAX(SALARY) AS MAXSALARY FROM TABLE GROUP BY FNAME, LNAME) AS B ON A.FNAME = A.FNAME AND A.LNAME = B.LNAME AND A.SALARY = B.MAXSALRY GROUP BY A.FNAME, A.LNAME Mark
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.