Solved

Access query - First keyword question

Posted on 2013-06-02
7
392 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
ID: 39214455
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
ID: 39214485
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.
ID: 39214518
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:terpsichore
ID: 39214525
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.
ID: 39214545
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
ID: 39214617
Very perceptive, very thorough.
0
 
LVL 38

Expert Comment

by:Jim P.
ID: 39214812
Thanks.

Glad to be of assistance. May your days get brighter and brighter.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

860 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