Solved

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

Posted on 2013-06-10
6
624 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 

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

Independent Software Vendors: 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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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…

713 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