[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
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
Medium Priority
?
821 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 70

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

656 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