Solved

Access07 - Query Max 2 items from another query

Posted on 2014-01-26
6
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

730 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