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
770 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

911 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