Create Query on max (1 Viewer)

unis7817

Registered User.
Local time
Today, 06:18
Joined
Apr 12, 2013
Messages
12
Hi,

I am new to access. I seek some help creating a query.

I have a table with field Date, Time, Traffic, cell, ...

I wish to create a query, which gives me one record from this table where Traffic is max, for that cell along with all other fields related to that record.

Date Time Cell Name Traffic TR PR CR DR RK
06-Mar-19 12:00:00 AM XX001A 11.9542 1 12 0 12 2
06-Mar-19 12:00:00 AM XX001B 11.7528 2 11 0 11 2
06-Mar-19 12:00:00 AM XX001C 11 2 11 0 11 2
07-Mar-19 12:00:00 AM XX001A 14 1 12 0 12 2
07-Mar-19 12:00:00 AM XX001B 11.0222 1 13 0 13 2
07-Mar-19 12:00:00 AM XX001C 11 3 11 0 11 2
08-Mar-19 12:00:00 AM XX001A 10.8222 2 11 0 11 2
08-Mar-19 12:00:00 AM XX001B 11 2 11 0 11 2
08-Mar-19 12:00:00 AM XX001C 10.6847 2 12 0 12 2
09-Mar-19 12:00:00 AM XX001A 12 1 12 0 12 2
09-Mar-19 12:00:00 AM XX001B 10.9944 3 11 0 11 2
09-Mar-19 12:00:00 AM XX001C 12.3639 1 12 0 12 2
10-Mar-19 12:00:00 AM XX001A 9.3329 3 10 0 10 2
10-Mar-19 12:00:00 AM XX001B 10.9819 2 11 0 11 2


Appreciate your help.

Thx,
 

isladogs

MVP / VIP
Local time
Today, 14:18
Joined
Jan 14, 2017
Messages
18,207
Create an aggregate query by clicking the Totals button on the ribbon.
Add all fields required to the query window. all will say Group By in the Totals row
Change the Total row entry for the Traffic field from Group By to Max.

However, depending n your requirements, you may need to reduce the fields included if you get multiple records for the same cell name. For example, you may need to omit the date & time fields from group by.
If so you then join the aggregate query to the original table to get the field values for date & time where the traffic is a maximum

Hope that helps

BTW - both date & time are reserved words and shouldn't be used for field names. Instead use e.g. TMDate, TMTime where TM stands for traffic measurement
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:18
Joined
May 7, 2009
Messages
19,227
1. create first a total query (qryDateMaxTraffic):
Code:
SELECT yourTable.date, 
         yourTable.time, Max(yourTable.traffic) AS MaxOftraffic
FROM yourTable
GROUP BY yourTable.date, yourTable.time;
2. create the final query (joining qryDateMaxTraffic to yourTable):
Code:
SELECT qryDateMaxTraffic.date, 
qryDateMaxTraffic.time, 
yourTable.cell, 
yourTable.name, 
qryDateMaxTraffic.MaxOftraffic AS Traffic, 
yourTable.tr, 
yourTable.pr, 
yourTable.cr, 
yourTable.dr, 
yourTable.rk
FROM qryDateMaxTraffic 
INNER JOIN yourTable 
ON (qryDateMaxTraffic.MaxOftraffic = yourTable.traffic) 
AND (qryDateMaxTraffic.time = yourTable.time) 
AND (qryDateMaxTraffic.date = yourTable.date);
 

unis7817

Registered User.
Local time
Today, 06:18
Joined
Apr 12, 2013
Messages
12
Create an aggregate query by clicking the Totals button on the ribbon.
Add all fields required to the query window. all will say Group By in the Totals row
...
Hope that helps
I tried to do this way, but i got stuck with double variable lookup. Because, I will need to lookup two variables from one with two variables from another to get rest of the fields. Ended up messing it.

BTW - both date & time are reserved words and shouldn't be used for field names. Instead use e.g. TMDate, TMTime where TM stands for traffic measurement
Thanks for the advice. I will try to change it.
 

unis7817

Registered User.
Local time
Today, 06:18
Joined
Apr 12, 2013
Messages
12
1. create first a total query (qryDateMaxTraffic):
Code:
SELECT yourTable.date, 
         yourTable.time, Max(yourTable.traffic) AS MaxOftraffic
FROM yourTable
GROUP BY yourTable.date, yourTable.time;
2. create the final query (joining qryDateMaxTraffic to yourTable):
Code:
SELECT qryDateMaxTraffic.date, 
qryDateMaxTraffic.time, 
yourTable.cell, 
yourTable.name, 
qryDateMaxTraffic.MaxOftraffic AS Traffic, 
yourTable.tr, 
yourTable.pr, 
yourTable.cr, 
yourTable.dr, 
yourTable.rk
FROM qryDateMaxTraffic 
INNER JOIN yourTable 
ON (qryDateMaxTraffic.MaxOftraffic = yourTable.traffic) 
AND (qryDateMaxTraffic.time = yourTable.time) 
AND (qryDateMaxTraffic.date = yourTable.date);

Thanks. I will try to implement it this way.
 

Users who are viewing this thread

Top Bottom