Link to home
Start Free TrialLog in
Avatar of wlwebb
wlwebbFlag for United States of America

asked on

Access07 - Query Max 2 items from another query

Hello all,
Last night I posted a question regarding pulling the last Item of all Items.  Got that to work...Thanks....  However, I took that theory and tried going to the next step

I have a query that is a combination of several tables.  I am wanting to get the Last input of each Item ([MachPollPositionID]).  I tried this
SELECT InfoDtailID, MachPollPositionID, MachPulledYN, MAX(MachPollPositionID) AS MaxPollPos, MAX(BusDay) AS MaxBusDay
FROM qry_InfoDetails_LastPull
WHERE (((qry_InfoDetails_LastPull.MachPulledYN)=TRUE))
GROUP BY qry_InfoDetails_LastPull.InfoDtailID, qry_InfoDetails_LastPull.MachPollPositionID, qry_InfoDetails_LastPull.MachPulledYN;

Open in new window


However, I am getting all the Items MachPollPositionID

My source query's data looks like this:

FIELDS (COLUMNS Below)
InfoDtailID
InfoID
MachPollPositionID
AmtIn
MachPulledYN
ShiftID
InfoDate
MachinePoll
BusDay

12      1      1      $0.00      Yes      1      1/1/2014 3:43:46 PM      1      1/1/2014
13      1      2      $0.00      Yes      1      1/1/2014 3:43:46 PM      2      1/1/2014
14      1      3      $0.00      Yes      1      1/1/2014 3:43:46 PM      3      1/1/2014
15      1      4      $0.00      Yes      1      1/1/2014 3:43:46 PM      4      1/1/2014
16      1      5      $0.00      Yes      1      1/1/2014 3:43:46 PM      5      1/1/2014
32      5      1      $0.00      Yes      6      1/22/2014 12:31:59 AM      1      1/21/2014
36      5      5      $0.00      Yes      6      1/22/2014 12:31:59 AM      5      1/21/2014

What I expected is that my new query would have pulled for PollPositionID the date:
1  1/21/2014
2  1/1/2014
3  1/1/2014
4  1/1/2014
5  1/21/2014

However, I'm simply ending up with the same query as my results above....It's not giving me the MAX date for each PollPositionID
ASKER CERTIFIED SOLUTION
Avatar of Lawrence Barnes
Lawrence Barnes
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SickSkilz
SickSkilz

Have you tried adding this to the where clause?
MachPollPositionID =MAX(MachPollPositionID)
Avatar of wlwebb

ASKER

LVBarnes
Uggh....So is there a way to get that InfoDtailID for the records that it does select?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wlwebb

ASKER

Thanks.... Looks like that got most of what I needed
Thanks for the help SickSkilz.

WlWebb I looked at your original query and modified it to give you the ID for the max day.  I'm not familiar with your data though so you'll want to verify the joins.
SELECT lp.MachPollPositionID, lp.MachPulledYN, lp.MaxPollPOS, lp.MaxBusDay, lpbid.InfoDtailID as MaxInfoDtailID
FROM (
SELECT MachPollPositionID, MachPulledYN, MAX(MachPollPositionID) AS MaxPollPos, MAX(BusDay) AS MaxBusDay
FROM qry_InfoDetails_LastPull
WHERE (((qry_InfoDetails_LastPull.MachPulledYN)=TRUE))
GROUP BY qry_InfoDetails_LastPull.MachPollPositionID, qry_InfoDetails_LastPull.MachPulledYN) lp
INNER JOIN (
SELECT InfoDtailID, MachPollPositionID, MachPulledYN, MAX(MachPollPositionID) AS MaxPollPosByID, MAX(BusDay) AS MaxBusDayByID
FROM qry_InfoDetails_LastPull
WHERE (((qry_InfoDetails_LastPull.MachPulledYN)=TRUE))
GROUP BY qry_InfoDetails_LastPull.InfoDtailID, qry_InfoDetails_LastPull.MachPollPositionID, qry_InfoDetails_LastPull.MachPulledYN) lpbid ON lp.MaxPollPOS=lpbid.MaxPollPOSByID AND lp.MaxBusDay=lpbid.MaxBusDayByID

Open in new window