I am obviously not understanding something very basic with regard to Group BY
and Max() sql statements.
All I want is to return the entry which has the greatest date value per unit
no. I have the following
SELECT unit_no, Max(eta_date) AS MaxOfeta_date, tbl_cngu_qt.eta_city
FROM tbl_qt
GROUP BY cngu_no, eta_city
ORDER BY cngu_no;
but it returns multiple entries per unit??
What is the proper synthax for this query?
QB
|
|
0
|
|
|
|
Reply
|
Utf
|
1/24/2010 1:34:01 AM |
|
I came up with the following. It seems to work, but would still like to know
if a) this is proper, b) if there is a more efficient method
SELECT Temp1.unit_no, Temp1.MaxOfeta_date AS eta_date, tbl_qt.eta_city
FROM [
SELECT tbl_qt.unit_no, Max(tbl_qt.eta_date) AS MaxOfeta_date
FROM tbl_qt GROUP BY tbl_qt.unit_no ORDER BY tbl_qt.unit_no
]. AS Temp1
LEFT JOIN tbl_qt ON (Temp1.MaxOfeta_date = tbl_qt.eta_date) AND
(Temp1.cngu_no = tbl_qt.unit_no);
QB
"QB" wrote:
> I am obviously not understanding something very basic with regard to Group BY
> and Max() sql statements.
>
> All I want is to return the entry which has the greatest date value per unit
> no. I have the following
>
> SELECT unit_no, Max(eta_date) AS MaxOfeta_date, tbl_cngu_qt.eta_city
> FROM tbl_qt
> GROUP BY unit_no, eta_city
> ORDER BY unit_no;
>
> but it returns multiple entries per unit??
>
> What is the proper synthax for this query?
>
> QB
|
|
0
|
|
|
|
Reply
|
Utf
|
1/24/2010 1:42:20 AM
|
|
QB,
If what you are trying to do is get the unit #, and the 1 city associated
with the Maximum (ETA_Date) for each unit, then your tecnique below would
work (although I would move the Order By clause out of the subquery and put
it in the outer part of the query. Another method to do this is:
SELECT Unit_No, Eta_Date, Eta_City
FROM tbl_qt
WHERE eta_Date = (SELECT Max(Eta_Date)
FROM tbl_qt as T
WHERE T.Unit_No = tbl_Qt.Unit_No)
Personally, I prefer the method you used, but its always good to know your
alternatives.
HTH
Dale
"QB" <QB@discussions.microsoft.com> wrote in message
news:319993B8-04DE-4F5F-BFC2-D24A1E75ACF5@microsoft.com...
>I came up with the following. It seems to work, but would still like to
>know
> if a) this is proper, b) if there is a more efficient method
>
> SELECT Temp1.unit_no, Temp1.MaxOfeta_date AS eta_date, tbl_qt.eta_city
> FROM [
> SELECT tbl_qt.unit_no, Max(tbl_qt.eta_date) AS MaxOfeta_date
> FROM tbl_qt GROUP BY tbl_qt.unit_no ORDER BY tbl_qt.unit_no
> ]. AS Temp1
> LEFT JOIN tbl_qt ON (Temp1.MaxOfeta_date = tbl_qt.eta_date) AND
> (Temp1.cngu_no = tbl_qt.unit_no);
>
> QB
>
>
>
> "QB" wrote:
>
>> I am obviously not understanding something very basic with regard to
>> Group BY
>> and Max() sql statements.
>>
>> All I want is to return the entry which has the greatest date value per
>> unit
>> no. I have the following
>>
>> SELECT unit_no, Max(eta_date) AS MaxOfeta_date, tbl_cngu_qt.eta_city
>> FROM tbl_qt
>> GROUP BY unit_no, eta_city
>> ORDER BY unit_no;
>>
>> but it returns multiple entries per unit??
>>
>> What is the proper synthax for this query?
>>
>> QB
|
|
0
|
|
|
|
Reply
|
Dale
|
1/24/2010 1:48:14 AM
|
|
Dale Fye's method is a good one if you need to be able to edit the records in
the query or if you have NOT followed the naming guidelines for tables and
fields (Letters, Numbers, and underscore characters only and not RESERVED
words such as Date).
Your original method is generally faster but I believe you will find that you
cannot edit the records returned by this query.
One additional option is to use nested queries if your field and table names
do not follow the naming guidelines.
Save a query as q1
SELECT tbl_qt.unit_no, Max(tbl_qt.eta_date) AS MaxOfeta_date
FROM tbl_qt
GROUP BY tbl_qt.unit_no
Now use that query in your subsequent query
SELECT Temp1.unit_no, Temp1.MaxOfeta_date AS eta_date, tbl_qt.eta_city
FROM Q1 AS Temp1
LEFT JOIN tbl_qt ON (Temp1.MaxOfeta_date = tbl_qt.eta_date) AND
(Temp1.cngu_no = tbl_qt.unit_no);
In your existing query you might drop the order by clause in the subquery as
Dale noted. Although it will probably be ignored since it is not needed for
the execution of the subquery.
And I would change the LEFT JOIN to an INNER JOIN. I don't KNOW that an INNER
JOIN is more efficient, but I SUSPECT that it might be and I can see no reason
not to use an INNER JOIN since there has to be a matching record.
SELECT Temp1.unit_no, Temp1.MaxOfeta_date AS eta_date, tbl_qt.eta_city
FROM [
SELECT tbl_qt.unit_no, Max(tbl_qt.eta_date) AS MaxOfeta_date
FROM tbl_qt GROUP BY tbl_qt.unit_no
]. AS Temp1
INNER JOIN tbl_qt ON (Temp1.MaxOfeta_date = tbl_qt.eta_date) AND
(Temp1.cngu_no = tbl_qt.unit_no)
QB
John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
Dale Fye wrote:
> QB,
>
> If what you are trying to do is get the unit #, and the 1 city associated
> with the Maximum (ETA_Date) for each unit, then your tecnique below would
> work (although I would move the Order By clause out of the subquery and put
> it in the outer part of the query. Another method to do this is:
>
> SELECT Unit_No, Eta_Date, Eta_City
> FROM tbl_qt
> WHERE eta_Date = (SELECT Max(Eta_Date)
> FROM tbl_qt as T
> WHERE T.Unit_No = tbl_Qt.Unit_No)
>
> Personally, I prefer the method you used, but its always good to know your
> alternatives.
>
> HTH
> Dale
>
>
> "QB" <QB@discussions.microsoft.com> wrote in message
> news:319993B8-04DE-4F5F-BFC2-D24A1E75ACF5@microsoft.com...
>> I came up with the following. It seems to work, but would still like to
>> know
>> if a) this is proper, b) if there is a more efficient method
>>
>> SELECT Temp1.unit_no, Temp1.MaxOfeta_date AS eta_date, tbl_qt.eta_city
>> FROM [
>> SELECT tbl_qt.unit_no, Max(tbl_qt.eta_date) AS MaxOfeta_date
>> FROM tbl_qt GROUP BY tbl_qt.unit_no ORDER BY tbl_qt.unit_no
>> ]. AS Temp1
>> LEFT JOIN tbl_qt ON (Temp1.MaxOfeta_date = tbl_qt.eta_date) AND
>> (Temp1.cngu_no = tbl_qt.unit_no);
>>
>> QB
>>
>>
>>
>> "QB" wrote:
>>
>>> I am obviously not understanding something very basic with regard to
>>> Group BY
>>> and Max() sql statements.
>>>
>>> All I want is to return the entry which has the greatest date value per
>>> unit
>>> no. I have the following
>>>
>>> SELECT unit_no, Max(eta_date) AS MaxOfeta_date, tbl_cngu_qt.eta_city
>>> FROM tbl_qt
>>> GROUP BY unit_no, eta_city
>>> ORDER BY unit_no;
>>>
>>> but it returns multiple entries per unit??
>>>
>>> What is the proper synthax for this query?
>>>
>>> QB
>
>
|
|
0
|
|
|
|
Reply
|
John
|
1/24/2010 3:12:26 PM
|
|
|
3 Replies
241 Views
(page loaded in 0.126 seconds)
Similiar Articles: Suppress printing group footer using Calculated field(s ...... of the 3 fields for a part and they come out great. ... Field 1) = MATERIAL_$ / QTY (Giving me the cost per unit ... Creating a calculated field using dates in a form ... Formula counting working days - microsoft.public.project ...... project start date (not each tasks start date ... use a custom duration field as you do (the best solution IMHO) because that is shown in whatever is your duration unit. MFC under VS2010 - microsoft.public.vc.mfc... or modern framework to develop in this > decade, but it's still the best MFC to date. ... with the Marketplace, I think that the $99 fee combined with a small per-unit ... Running Total from a Subform - microsoft.public.access.forms ...I have a unbound control box (called textbox 20) in the details section on my subform that calculates unit price and units orders. The data source ... The best way to display resources in the Task Sheet - microsoft ...... that you may have them available as per ... as in a company, where having one unit take ... MSProject 2003 Kicking End Dates Way Out - microsoft.public ... The best way to ... show 2nd field when combo box choice is made - microsoft.public ...Best, Scott Jeanette Cunningham wrote: >In the after ... another way of doing this by looking up the unit price ... In your case you are allowing only one product per ... formulas in standard columns - microsoft.public.project ...Your duration, start/finish dates and bar on Gantt chart will now change automatically when you change quantity and/or unit rate. ... fragile and if not handled with great ... DLookup within Iif statement - microsoft.public.access.queries ...Syntax error in date in query expression issue - microsoft ... ... an action ... out the Day function ... earned per ... But within VB6 and Access I do my absolute best to ... up ... trouble with 640GB laptop drive - microsoft.public.win98.gen ...There is an updated Fdisk.exe, which has a file date of ... lets me set a custom cluster size (allocation unit size ... Using the the cumulative 98 Guy activities per this ... Putting a hold on tasks - microsoft.public.projectFor the second task, the date marking the end of the work performed > populates the Stop field and the date the remaining work begins populates > the Resume field. Name: Date: Per1 Name:_____ Date:_____ Per_____ Unit 10: The Great Gatsby (I think you’ll enjoy this unit, Old Sport!) Medicaid Drug Rebate Program | Medicaid.gov... Clotting Factors – the greater of 17.1 % of the AMP per unit or the difference between the AMP and the best price per unit and adjusted by the CPI-U based on launch date ... Unit price - Wikipedia, the free encyclopediaAverage price per unit and prices per statistical unit are needed by marketers ... sellers and finds that Seller Three offers widgets at the best value, the best price. Unit ... Insolation - Wikipedia, the free encyclopediaPractitioners in the business of solar energy may use the unit Wh/m 2 (watt-hours per square meter). ... SMARTS, software to compute solar insolation of each date/location of ... Gold PriceYou can also find out where to buy gold coins from gold dealers at the best gold ... Gold Price per Ounce Gold Price per Gram Gold Price per Kilo Gold Price History 7/22/2012 3:35:24 PM
|