2011-11-08 75 views
0

我正在爲我的數據分頁使用this問題的解決方案。複雜的SQL分頁查詢

我現在需要將此解決方案用於更復雜的查詢。 IE瀏覽器。括號內的SELECT具有連接和聚合功能。

這是一個解決方案,我現在把它用作參考:

;WITH Results_CTE AS 
(
    SELECT 
     Col1, Col2, ..., 
     ROW_NUMBER() OVER (ORDER BY SortCol1, SortCol2, ...) AS RowNum 
    FROM Table 
    WHERE <whatever> 
) 
SELECT * 
FROM Results_CTE 
WHERE RowNum >= @Offset 
AND RowNum < @Offset + @Limit 

,我需要納入上述溶液查詢:

SELECT users.indicator, COUNT(*) as 'queries' FROM queries 
INNER JOIN calls ON queries.call_id = calls.id 
INNER JOIN users ON calls.user_id = users.id 
WHERE queries.isresolved=0 AND users.indicator='ind1' 
GROUP BY users.indicator ORDER BY queries DESC 

我怎樣才能做到這一點?到目前爲止,我已通過刪除ORDER BY queries DESC部分並將其放入ROW_NUMBER() OVER (ORDER BY ...) AS RowNum行,但是當我這樣做時,它不允許我按該列進行排序(「列名無效」查詢「)。

我需要做什麼才能讓它按此專欄排序?

編輯:使用SQL Server 2008

回答

1

嘗試ORDER BY COUNT(*)DESC。它適用於MySQL的...不知道有關SQL Server 2008

+0

憑啥沒」我試試這個。作品,非常感謝。 8分鐘後,我可以將其標記爲答案 – user982119

0

我想查詢你的別名爲COUNT(*)列 然後使用這樣

SELECT users.indicator, COUNT(*) as 'queries' FROM queries 
INNER JOIN calls ON queries.call_id = calls.id 
INNER JOIN users ON calls.user_id = users.id 
WHERE queries.isresolved=0 AND users.indicator='ind1' 
GROUP BY users.indicator ORDER BY COUNT(*) DESC 

http://oops-solution.blogspot.com/2011/11/string-handling-in-javascript.html