我正在尝试解决一个SQL Server错误。有人可以帮我吗?
查询是:
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;
错误:
除非还指定了TOP或FOR XML,否则ORDER BY子句在视图,内联函数,派生表,子查询和公用表表达式中无效。
如前所述,除非使用TOP或FOR XML,否则ORDER BY不得在子查询中使用。
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;