SQL Problem “The ORDER BY clause is invalid in views…”

邮差的信 提交于 2019-12-12 12:24:49

问题


I have a SQL Server error I'm trying to resolve. Could someone please help me out?

The query is:

SELECT TOP 10 * 
FROM ( 
SELECT c.id, c.name, c.inserteddate, c.cityname, ftblstates.name AS statename, clc.name AS catname, '' AS listingimagelogo, '' AS orgname, relocateyn, '' AS employerclassified
FROM ((tblclassifieds c 
LEFT JOIN tblclassifiedscategories clc ON c.categoryid = clc.id) 
LEFT JOIN ftblstates ON c.stateid = ftblstates.id) 
WHERE (c.expirydate != '') AND NOT c.id IN ( 
SELECT TOP 10 tblclassifieds.id 
FROM tblclassifieds 
WHERE (c.expirydate != '') 
ORDER BY inserteddate desc) 
UNION ALL 
SELECT ce.id, ce.name, ce.inserteddate, suburb AS cityname, ftblstates.name AS statename, ce.jobtype AS catname, ce.listingimagelogo, ce.orgname, '' AS relocateyn, '1' AS employerclassified 
FROM tblclassifiedemployers ce 
LEFT JOIN ftblstates ON ce.stateid = ftblstates.id 
WHERE (ce.expirydate != '') AND NOT ce.id IN ( 
SELECT TOP 10 tblclassifiedemployers.id 
FROM tblclassifiedemployers 
WHERE (ce.expirydate != '') 
ORDER BY inserteddate desc) 
ORDER BY inserteddate desc; 

And the error: The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.


回答1:


As stated ORDER BY must not apper in subqueries unless TOP or FOR XML is used.

    SELECT TOP 10 * FROM ( 
    SELECT 
        c.id, 
        c.name, 
        c.inserteddate, 
        c.cityname, 
        ftblstates.name AS statename, 
        clc.name AS catname, 
        '' AS listingimagelogo, 
        '' AS orgname, relocateyn, 
        '' AS employerclassified
    FROM tblclassifieds c 
    LEFT JOIN tblclassifiedscategories clc ON c.categoryid = clc.id
    LEFT JOIN ftblstates ON c.stateid = ftblstates.id 
    WHERE c.expirydate != ''
    AND NOT c.id IN ( 
        SELECT TOP 10 
            tblclassifieds.id 
        FROM tblclassifieds 
        WHERE c.expirydate != ''
        ORDER BY inserteddate desc 
    ) 
    UNION ALL
    SELECT 
        ce.id, 
        ce.name, 
        ce.inserteddate, 
        suburb AS cityname, 
        ftblstates.name AS statename, 
        ce.jobtype AS catname, 
        ce.listingimagelogo, 
        ce.orgname, '' AS relocateyn, 
        '1' AS employerclassified 
    FROM tblclassifiedemployers ce 
    LEFT JOIN ftblstates ON ce.stateid = ftblstates.id 
    WHERE ce.expirydate != ''
    AND NOT ce.id IN ( 
        SELECT TOP 10 
            tblclassifiedemployers.id 
        FROM tblclassifiedemployers 
        WHERE ce.expirydate != ''
        ORDER BY inserteddate desc
    )
) a ORDER BY inserteddate desc;



回答2:


If you are using SQL Server 2012 or higher version, please use "offset 0 rows" after order by. Ex -

create view Department_View
as
select Name from [HumanResources].[Department]
order by Name offset 0 rows



回答3:


I used the following construction:

SELECT
    ROW_NUMBER() OVER (ORDER BY LASTNAME) SORTORDER,
    *
FROM
    CLIENT



回答4:


I think it is self-explanatory. You can't use the ORDER BY clause in your sub-queries because they don't use a TOP or FOR XML.

When reviewing your code, I'm not sure I see the problem code though. All of the code looks correct. You are only using the ORDER BY in sub-queries that also employe the TOP command so they should all work. I would comment out all of the ORDER BY commands and then add them in one at a time until you find the one that throws the error. Maybe it doesn't like them because they are a sub-query of a sub-query.

Edit: Looking over it again, I think the problem is your IN statements. I don't believe you can use a sub-query with the ORDER BY when you are using IN.

Here is the code I would try:

SELECT TOP 10 * 
FROM ( 
SELECT c.id, c.name, c.inserteddate, c.cityname, ftblstates.name AS statename, clc.name AS catname, '' AS listingimagelogo, '' AS orgname, relocateyn, '' AS employerclassified
FROM ((tblclassifieds c 
LEFT JOIN tblclassifiedscategories clc ON c.categoryid = clc.id) 
LEFT JOIN ftblstates ON c.stateid = ftblstates.id) 
WHERE (c.expirydate != '') AND NOT c.id IN ( 
SELECT TOP 10 tblclassifieds.id 
FROM tblclassifieds 
WHERE (c.expirydate != '')) 
UNION ALL 
SELECT ce.id, ce.name, ce.inserteddate, suburb AS cityname, ftblstates.name AS statename, ce.jobtype AS catname, ce.listingimagelogo, ce.orgname, '' AS relocateyn, '1' AS employerclassified 
FROM tblclassifiedemployers ce 
LEFT JOIN ftblstates ON ce.stateid = ftblstates.id 
WHERE (ce.expirydate != '') AND NOT ce.id IN ( 
SELECT TOP 10 tblclassifiedemployers.id 
FROM tblclassifiedemployers 
WHERE (ce.expirydate != '')))



回答5:


CREATE FUNCTION GetUnitIDWithScenarioCount
(
@calculatorType int
)
returns TABLE as
return
(
select count,unitid from(
 SELECT Top 1
         count(sc.UnitId) as count, sc.unitid
           FROM scenarios SC 
    INNER JOIN npcstatus NPC
        ON NPC.UnitId=SC.UnitId
    INNER JOIN IPEDSCollegeData..hd hd
        ON hd.UnitId=NPC.UnitId
        WHERE npc.calculatorType=4
        Group by sc.unitid
    ) as temp
    order by count
    )


来源:https://stackoverflow.com/questions/5945851/sql-problem-the-order-by-clause-is-invalid-in-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!