Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-06-10
6
Medium Priority
?
636 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
[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
  • 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 

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

Stack Overflow Podcast - Developer Story

Welcome to the Stack Overflow podcast recorded Thursday July 20 at Stack Overflow Headquearters in NYC. Your hosts today are podcast regulars Jay Hanlon, David Fullerton, and Ilana Yitzhaki, plus the quite irregular Matt Sherman (Stack Overflow Engineering Manager extraordinaire)

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

688 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