Solved

Access query - "Top 1" record anomaly (?)

Posted on 2013-06-10
6
587 Views
Last Modified: 2013-06-11
I just had a query question answered nicely, but it now created a different issue.
The query now outputs a "TOP 1" set of fields that are correct, and match a given Part_ID.

However, I want to be able to give these fields for EACH part on a project - So I was doing a LEFT JOIN between the parts table and the query table (which outputs a TOP 1  result).

Instead of giving the different answer for each part, it only output a single answer, for 1 record.

any ideas of how to fix?
Thanks, experts!
0
Comment
Question by:terpsichore
  • 3
  • 2
6 Comments
 
LVL 57
ID: 39234427
Check out the following MSKB article:

ACC: How to Create a Top N Values per Group Query
http://support.microsoft.com/kb/153747

Jim.
0
 
LVL 9

Expert Comment

by:armchair_scouse
ID: 39234434
TOP 1 in the SQL statement will only yield you the single answer, as the command implies...  perhaps you could create several SQL statements, one for each part of your project and then use the UNION command to chain them together, each separate part having a 'TOP 1' in it?

For example:

SELECT TOP 1 * FROM tblDeals WHERE strSedol='0004659'
UNION
SELECT TOP 1 * FROM tblDeals WHERE strSedol='0009137'

Open in new window


Edit: Just seen JDettman's answer - go with his advice :o)
0
 
LVL 57
ID: 39234437
I just looked back and didn't understand the question correctly.

<<The query now outputs a "TOP 1" set of fields that are correct, and match a given Part_ID.>>

  Does it do it only for a single part, or is it doing a TOP 1 for each part within a group.

Jim.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:terpsichore
ID: 39234455
It is outputting only the result for a single part.
This is the current Query, which is working OK for a single part:

SELECT TOP 1 PP.Part_ID, R.ExpDate, M.Description
FROM Project_Parts AS PP INNER JOIN (Project_Parts_Revenue AS R INNER JOIN PICK_PartMilestoneType AS M ON R.Milestone_ID = M.ID) ON PP.Part_ID = R.Part_ID
WHERE (((R.Complete)=False) AND ((PP.Canceled)=False))
ORDER BY PP.Part_ID, R.ExpDate, R.RevenueID;

I need to invoke this, as noted, across a range of Part_ID's...
THANK YOU
0
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
ID: 39234667
You need to do this a bit differently as your TOP1 query is looking at all parts.  It's not filtering on Project at all.

1. What you want is the last part on a project where R.Complete=False AND PP.Canceled)=False right?

  So first, create a new query.    Add in Project_Parts and Project_Parts_Revenue tables.

 Now make it a GROUP BY (hit the "E" on the toolbar).  

 Pull down the project, part, r.Complete, PP.Canceled, R.ExpDate, and R.RevenueID

  Under the project column set the Total line to "GROUP BY".   Under ExpDate, sort ascending and put LAST on the total line.  Put LAST on the Part ID Column as well.

  Under the complete and Canceled colums, change the total Line to a WHERE condition.  You'll have to uncheck the output box for these.

  Now execute this.  You should have one row per project with the first part where canceled and complete columns are false.

 Save this.

 Now use this in your other query just as you were trying to do with the TOP1.

Jim.
0
 

Author Closing Comment

by:terpsichore
ID: 39237065
many sincere thanks.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

743 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