Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-06-10
6
Medium Priority
?
643 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 58
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 58
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 58

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

879 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