Solved

Access query - First keyword question

Posted on 2013-06-02
7
383 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access 2016 - Labels prompt to print 4 32
SQL Error - Query 6 26
Separate 2 comma delimited columns into separate rows 2 32
MS Access Delete All Excel sheets 1 10
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
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 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…

803 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