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
793 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3
10 Comments
 
LVL 69

Expert Comment

by:Scott Pletcher
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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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
 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

733 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