2013-03-02 35 views
0

即時通訊使用此查詢atm通過我想訂購TagReplies。但是,當它不會允許我保存它,除非我指定頂部或之後第二次選擇,但我想要所有結果。沒有頂部的sql語法

AS 
SELECT 
Tag, 
TagReplies, 
(TagReplies*1.00) AS Weight, 
TagTopics, 
(TagTopics*1.00) AS Weight2 

FROM 
(
SELECT 
    Tags.Tag 
    ,SUM(T.TopicReplies) As TagReplies 
    ,Count(T.TopicId) As TagTopics 
FROM 
     Tags 
    INNER JOIN Topics T ON Tags.TopicId = T.TopicId 
GROUP BY 
    Tags.Tag 
if add this eg ORDER BY TagReplies DESC 
) T 

它工作正常,如果我加入了最

+0

爲什麼你需要派生表中的ORDER BY? – 2013-03-02 04:19:24

回答

2

閱讀您收到錯誤消息。子查詢中不能使用ORDER BY。整個查詢只能使用ORDER BYORDER BYTOP一起作爲子查詢中的特例,因爲它決定了什麼記錄構成TOP

試試這個:

SELECT 
Tag, 
TagReplies, 
(TagReplies*1.00) AS Weight, 
TagTopics, 
(TagTopics*1.00) AS Weight2 

FROM 
(
SELECT 
    Tags.Tag 
    ,SUM(T.TopicReplies) As TagReplies 
    ,Count(T.TopicId) As TagTopics 
FROM 
     Tags 
    INNER JOIN Topics T ON Tags.TopicId = T.TopicId 
GROUP BY 
    Tags.Tag 
) T 
ORDER BY TagReplies DESC 

此外,它看起來像這可能是一個VIEW定義的一部分。您不能在VIEW定義中指定ORDER BY子句。你必須在SELECTVIEW