Solved

Access07 - Query Max 2 items from another query

Posted on 2014-01-26
6
294 Views
Last Modified: 2014-01-26
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
0
Comment
Question by:wlwebb
  • 2
  • 2
  • 2
6 Comments
 
LVL 5

Accepted Solution

by:
Lawrence Barnes earned 400 total points
ID: 39809991
It looks like you are grouping on the InfoDtailID...this is different in each table right?  You will have to remove it from your final query if you want the last date across all tables.
0
 
LVL 2

Expert Comment

by:SickSkilz
ID: 39809997
Have you tried adding this to the where clause?
MachPollPositionID =MAX(MachPollPositionID)
0
 

Author Comment

by:wlwebb
ID: 39809998
LVBarnes
Uggh....So is there a way to get that InfoDtailID for the records that it does select?
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 2

Assisted Solution

by:SickSkilz
SickSkilz earned 100 total points
ID: 39810010
Yes, but you need to do it as 2 query steps.  Step 1, get the query to return the uniquly identifying fields (id and maybe date).  Step 2, join the results of that query to this one to get the full details of those records.
0
 

Author Closing Comment

by:wlwebb
ID: 39810015
Thanks.... Looks like that got most of what I needed
0
 
LVL 5

Expert Comment

by:Lawrence Barnes
ID: 39810076
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

0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

910 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

25 Experts available now in Live!

Get 1:1 Help Now