Solved

Access query - First keyword question

Posted on 2013-06-02
7
358 Views
Last Modified: 2013-06-02
Dear experts -
I have a table of milestones for each milestone of a project. I want to determine the EARLIEST INCOMPLETE record in this table.
What I've done so far:
Create a Subquery, sorting the results by project + date + descriptor.
I created a master query that invokes the subquery and uses the FIRST keyword to find the first occurrence for this project_ID. (I assumed that it was using the sort order built into the subquery.)
It is simply pulling the wrong information.
Any ideas?
Thanks!
0
Comment
Question by:terpsichore
  • 4
  • 3
7 Comments
 
LVL 38

Accepted Solution

by:
Jim P. earned 500 total points
Comment Utility
You'll need to add an autonumber column to the table like ID.

Then you query will  look something like this:

SELECT SubQ.project, SubQ.datefld, SubQ.descriptor
FROM (SELECT TableX.project, TableX.datefld, TableX.descriptor,  (select count(*) from tableX as x where x.project=tablex.project and x.datefld=tablex.datefld and x.ID<=tablex.ID) AS Seq
   FROM TableX
   WHERE descriptor = "Incomplete"
   ORDER BY TableX.project, TableX.datefld, TableX.descriptor, TableX.ID) SubQ
WHERE SubQ.Seq = 1

Open in new window

0
 

Author Comment

by:terpsichore
Comment Utility
thanks - can you explain this a little more, and why this would work and why my version wouldn't, perhaps? THANK YOU
0
 
LVL 38

Expert Comment

by:Jim P.
Comment Utility
Basically I'm creating a sequence number. And it is a good idea to learn how to do it as the First and Last keywords are unique to Access/JET SQL and nothing else.

I presume that the data is added to you table at random times. This puts them in order and only select the sequence number 1.

That is based on the idea you have an index on the project and datefld when you add the autonumber.
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 

Author Comment

by:terpsichore
Comment Utility
great - sorry to trouble, but one last question before i award you the points - we are migrating to sql server (and also need a consultant for that!) - should we use a different query? THANK YOU
0
 
LVL 38

Expert Comment

by:Jim P.
Comment Utility
should we use a different query?

It is easier in MS SQL.

To get a sequence number you use the Row_Number or  Rank functions.
0
 

Author Closing Comment

by:terpsichore
Comment Utility
Very perceptive, very thorough.
0
 
LVL 38

Expert Comment

by:Jim P.
Comment Utility
Thanks.

Glad to be of assistance. May your days get brighter and brighter.
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

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
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…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
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.

771 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