我需要通過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'.
我做錯了嗎?由於
酷!謝謝! – user1947702