Solved

Access07 - Query Max 2 items from another query

Posted on 2014-01-26
6
292 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
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.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

744 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

12 Experts available now in Live!

Get 1:1 Help Now