(按案例排序)按子查询排序

时间:2014-02-05 20:44:43

标签: sql sql-server tsql

我需要通过int列升序来排序我的结果,但我想只获取带有数字(0 ... 10000)的行,但是默认排序为数字之前的列提供了具有空值的行。我google了解决方案,将行设置为null到排序结束(在所有数字之后)它看起来像

SELECT ProductName
FROM Products
ORDER BY
  CASE WHEN Position is null THEN 1 ELSE 0 END,
  Position

所以我的查询如下:

SELECT  c.CompanyId, c.CompanyName, c.CompanyCategoryId, cc.CompanyCategoryName, c.HQCountryISO, c.CrunchBaseUrl,c.AngelListUrl, 
(SELECT MAX(mf.NumLikes) FROM MeasurementFacebook mf
JOIN FacebookAccount f ON f.CompanyId = c.CompanyId
WHERE f.FacebookAccountId in (mf.FacebookAccountId)) as Likes,
(SELECT MAX(mt.NumFollowers) FROM MeasurementTwitter mt
JOIN TwitterAccount t ON t.CompanyId = c.CompanyId
WHERE t.TwitterAccountId in (mt.TwitterAccountId)) as Followers,
(SELECT MAX(ma.AlexaRanking) FROM MeasurementAlexa ma
JOIN Website w ON w.CompanyId = c.CompanyId
WHERE w.WebsiteId in (ma.WebsiteId)) as AlexaRank
FROM Company c 
JOIN CompanyCategory cc ON c.CompanyCategoryId = cc.CompanyCategoryId 
WHERE c.HQCountryISO =  'FRA' 
ORDER BY CASE  WHEN AlexaRank IS NULL THEN 1 ELSE 0 END, AlexaRank 
OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY 

如您所见,AlexaRank是第三个子查询的结果,我想通过此列对结果进行排序。但我有一个错误,上面写着:

Msg 207, Level 16, State 1, Line 14
Invalid column name 'AlexaRank'.

我做错了什么?感谢

2 个答案:

答案 0 :(得分:2)

虽然可以在ORDER BY子句中使用别名,但是不能在表达式中使用别名,最简单的解决方法是将其放在cte /子查询中:

;WITH cte AS (SELECT  c.CompanyId
                      , c.CompanyName
                      , c.CompanyCategoryId
                      , cc.CompanyCategoryName
                      , c.HQCountryISO
                      , c.CrunchBaseUrl
                      ,c.AngelListUrl
                      ,(SELECT MAX(mf.NumLikes) 
                        FROM MeasurementFacebook mf
                        JOIN FacebookAccount f ON f.CompanyId = c.CompanyId
                        WHERE f.FacebookAccountId in (mf.FacebookAccountId)) as Likes
                      ,(SELECT MAX(mt.NumFollowers) 
                        FROM MeasurementTwitter mt
                        JOIN TwitterAccount t ON t.CompanyId = c.CompanyId
                        WHERE t.TwitterAccountId in (mt.TwitterAccountId)) as Followers
                      ,(SELECT MAX(ma.AlexaRanking) 
                        FROM MeasurementAlexa ma
                        JOIN Website w ON w.CompanyId = c.CompanyId
                        WHERE w.WebsiteId in (ma.WebsiteId)) as AlexaRank
                FROM Company c 
                JOIN CompanyCategory cc ON c.CompanyCategoryId = cc.CompanyCategoryId 
                WHERE c.HQCountryISO =  'FRA')
SELECT * 
FROM  cte
ORDER BY CASE WHEN AlexaRank IS NULL THEN 1 ELSE 0 END, AlexaRank 
OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY 

答案 1 :(得分:0)

代码效率很低但您可以执行以下操作。基本上将您的初始查询包装在公用表表达式中,这样您就不需要按顺序重写第3个子选择。

SELECT * FROM (
SELECT c.companyid, 
   c.companyname, 
   c.companycategoryid, 
   cc.companycategoryname, 
   c.hqcountryiso, 
   c.crunchbaseurl, 
   c.angellisturl, 
   (SELECT Max(mf.numlikes) 
    FROM   measurementfacebook mf 
           JOIN facebookaccount f 
             ON f.companyid = c.companyid 
    WHERE  f.facebookaccountid IN ( mf.facebookaccountid )) AS Likes, 
   (SELECT Max(mt.numfollowers) 
    FROM   measurementtwitter mt 
           JOIN twitteraccount t 
             ON t.companyid = c.companyid 
    WHERE  t.twitteraccountid IN ( mt.twitteraccountid ))   AS Followers, 
   (SELECT Max(ma.alexaranking) 
    FROM   measurementalexa ma 
           JOIN website w 
             ON w.companyid = c.companyid 
    WHERE  w.websiteid IN ( ma.websiteid ))                 AS AlexaRank 
FROM   company c 
   JOIN companycategory cc 
     ON c.companycategoryid = cc.companycategoryid 
WHERE  c.hqcountryiso = 'FRA' ) Q
ORDER  BY CASE 
        WHEN Q.AlexaRank IS NULL THEN 1 
        ELSE 0 
      END, 
      Q.AlexaRank