• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 826
  • Last Modified:

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

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
dba123
Asked:
dba123
  • 2
  • 2
  • 2
  • +3
1 Solution
 
Scott PletcherSenior DBACommented:
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
 
alexgudCommented:
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
 
alexgudCommented:
sorry I typed my answer the same time ScottPletcher  did.
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
Anthony PerkinsCommented:
>>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
 
LowfatspreadCommented:
Please tell us what you trying to achieve with the query...

0
 
dba123Author Commented:
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
 
dba123Author Commented:
thanks for the inputs...sorry for the inconvenience.
0
 
Anthony PerkinsCommented:
>>sorry for the inconvenience.<<
It's Ok we are used to it. :)
0
 
ee_ai_constructCommented:
Closed, 500 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Moderator
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 2
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now