Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL: "ORDER BY clause conflicts with DISTINCT" please help

Status
Not open for further replies.

Arrowx7

Programmer
Feb 16, 2005
17
CA
Hello,
I've been at this for many hours, if you can give me help I would greatly appreciate it. This is the query:

SELECT DISTINCT tblPersonnel.PID, (select distinct top 1 tblAppointments.rank from tblAppointments where tblAppointments.PID=tblPersonnel.PID order by tblAppointments.start) AS Expr1
FROM tblPersonnel;

Error: ORDER BY clause conflicts with DISTINCT


The problem is in the subquery, I wanted to return the oldest record in the table, but I only want it to return "rank" field.

Here is what I tried:

If I remove Distinct from the subquery i get "only one record can be returned from the subquery" even though it says "top 1"

If I change tblAppointments.rank to tblAppointments.start it works perfectly, but it outputs the start date as opposed to the rank field.

If I put both rank and start in, it complains saying subquery can only return one field

I dont' know what else to try, please help!
 
Do you get any records returned from:
Code:
SELECT PID, Start
FROM tblAppointments
GROUP BY PID, Start
HAVING COUNT(Rank) > 1
If so, you need to break the tie by include the Rank in the ORDER BY of the subquery and removing the DISTINCT.


Duane
Hook'D on Access
MS Access MVP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top