Solved

The ORDER BY clause is invalid in views, inline functions, derived tables, and subqueries, unless TOP is also specified.

Posted on 2004-07-30
10
763 Views
Last Modified: 2008-02-01
Error:

The ORDER BY clause is invalid in views, inline functions, derived tables, and subqueries, unless TOP is also specified.

SQL:

SELECT MIN(Start_Date) as MinDate, MAX(End_Date) as MaxDate FROM Query_Registration where start_date IN ( SELECT Registration_ID, Course_ID, Course_Number, Course_Name, Class_ID, Start_Date, End_Date, User_ID, FName + ' ' + LName AS Name, Company_Name, Address1, Address2, City, State, Zip, Country, CC_Name, Price, Payment_Type_ID, Payment_Type_Name, CC_Type, CC_Approval_Code, CC_Amount, PO_Amount, PO_Number, Discount_Percent, Date_Paid FROM Query_Registration WHERE Course_ID IS NOT NULL AND Paid = 1 AND (PO_Amount IS NOT NULL OR CC_Amount IS NOT NULL) ORDER BY FName, Start_Date

0
Comment
Question by:dba123
  • 2
  • 2
  • 2
  • +3
10 Comments
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 11681898
This looks identical to an earlier question.

Aside from the ORDER BY issue, the request you are making is impossible.  You do an equal comparison of the "start_date" to more than two dozen different columns ("IN" is just of a short of = first_value or = second_value or ...).  It could not possibly be equal to all those columns at once.

If you just want to compare values using an IN, you don't need to ORDER the results in the IN.  You only need an ORDER BY in the final results, that is, the outermost SELECT.

If you just want a final result of just two dates, something like this query should work:

SELECT MIN(Start_Date) as MinDate, MAX(End_Date) as MaxDate
FROM Query_Registration
WHERE start_date IN (
    SELECT Start_Date
    FROM Query_Registration
    WHERE Course_ID IS NOT NULL AND Paid = 1 AND (PO_Amount IS NOT NULL OR CC_Amount IS NOT NULL))


If you want detail records for all rows that have the minimum starting date that match the selection criteria in the inner query, then you can do something like this:

SELECT Registration_ID, Course_ID, Course_Number, Course_Name,
      Class_ID, Start_Date, End_Date, User_ID, FName + ' ' + LName AS Name,
      Company_Name, Address1, Address2, City, State, Zip, Country, CC_Name,
      Price, Payment_Type_ID, Payment_Type_Name, CC_Type, CC_Approval_Code,
      CC_Amount, PO_Amount, PO_Number, Discount_Percent, Date_Paid
FROM Query_Registration
WHERE Course_ID IS NOT NULL AND Paid = 1 AND
      (PO_Amount IS NOT NULL OR CC_Amount IS NOT NULL) AND
      start_date = (
            SELECT MIN(start_date)
            FROM Query_Registration            
            WHERE Course_ID IS NOT NULL AND Paid = 1 AND
                  (PO_Amount IS NOT NULL OR CC_Amount IS NOT NULL))
ORDER BY FName, Start_Date


Sorry, it's hard to be more specific since I don't know exactly what result you are after and the criteria to be used to get that result.
0
 
LVL 4

Expert Comment

by:alexgud
ID: 11681905
first of all  "start_date IN (SELECT Start_Date ..." imbeded Select should return one or many records fot one field not the whole list of feilds. Like this:

SELECT MIN(Start_Date) as MinDate, MAX(End_Date) as MaxDate FROM Query_Registration WHERE start_date IN ( SELECT Start_Date FROM Query_Registration WHERE Course_ID IS NOT NULL AND Paid = 1 AND (PO_Amount IS NOT NULL OR CC_Amount IS NOT NULL))
ORDER BY FName, Start_Date

And then why do you want to sort it by fields you do not use for return?
0
 
LVL 4

Expert Comment

by:alexgud
ID: 11681908
sorry I typed my answer the same time ScottPletcher  did.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 11682126
>>This looks identical to an earlier question.<<
Here it is:
http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_21077470.html

I suspect he/she did not like the answer there.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 50

Expert Comment

by:Lowfatspread
ID: 11683338
Please tell us what you trying to achieve with the query...

0
 
LVL 1

Author Comment

by:dba123
ID: 11731348
My code is different slightly and I have a different error here.  That is why I posted a new thread.  The error pertains to something totally different than what I was trying to do in an ealier post so rather than confuse the hell out of everyone there, I moved on and changed the code and this is the error I can't get past now.


I am just trying to grab the max and min dates from a string that was already there by a previous coder.  I did resolve it...so I'd like to close this out.  I wasn't trying to match criteria, simply pull the max and min from the subquery so that is why it was sorted by ID..because it needs to be for other purposes of the app that you can't see.

I'd like to delete this one since I did resolve it.
0
 
LVL 1

Author Comment

by:dba123
ID: 11731363
thanks for the inputs...sorry for the inconvenience.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 11735933
>>sorry for the inconvenience.<<
It's Ok we are used to it. :)
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 11764205
Closed, 500 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Moderator
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

760 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

20 Experts available now in Live!

Get 1:1 Help Now