2014-02-05 42 views
1

我需要通過int列升序來排序我的結果,但我只想獲得具有數字的行(0 ... 10000),但默認排序爲我提供了具有空值的行數字之前的列。我用Google搜索解決方案,它設置爲null行插入排序結束(所有數字後),它看起來像(ORDER BY CASE WHEN)通過子查詢排序

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

雖然你可以在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 
+0

酷!謝謝! – user1947702

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