2010-11-08 156 views
1

我有以下查詢,它給了我一個錯誤:「關鍵字'GROUP'附近的語法不正確。」 < - 顯示第二個GROUP BY語句...您能告訴我爲什麼嗎?sql server 2005查詢語法錯誤

SELECTventId 
,TargetUser 
,TargetDomain 
,String05 
,Amount 
,max(Amount) maximum 
FROM 
    (SELECT EventId 
       , TargetUser 
       , TargetDomain 
      , String05 
FROM AdtServer.dvAll 
GROUP BY EventId, TargetUser, TargetDomain, String05) 

GROUP BY EVENTID,TargetUser,TargetDomain,String05,金額//錯誤所示的是

ORDER BY最大,TargetUser

如果我創建視圖的內部查詢,它工作正常(但我不不想創建視圖,因爲如果我的查詢是長時間運行的查詢,它會讓我超時)。

回答

1

你需要給一個別名到您的表:

SELECTventId 
,TargetUser 
,TargetDomain 
,String05 
,Amount 
,max(Amount) maximum 
FROM 
    (SELECT EventId 
       , TargetUser 
       , TargetDomain 
      , String05 
    FROM AdtServer.dvAll 
    GROUP BY EventId, TargetUser, TargetDomain, String05 
    ) x 
GROUP BY EventId,TargetUser,TargetDomain,String05,Amount 
ORDER BY maximum,TargetUser 
+0

你是正確的我找到了答案,但因爲你是絕對正確的,我會給你接受..非常感謝你.. :) – kupa 2010-11-08 13:50:57

1

我發現answeeeeeeeer

我應該寫as tempTB我的意思是我的查詢應該是這樣的:

SELECTventId 
,TargetUser 
,TargetDomain 
,String05 
,Amount 
,max(Amount) maximum 
FROM 
    (SELECT EventId 
       , TargetUser 
       , TargetDomain 
      , String05 
FROM AdtServer.dvAll 
GROUP BY EventId, TargetUser, TargetDomain, String05) as tempTB 
GROUP BY EventId,TargetUser,TargetDomain,String05,Amount 

ORDER BY maximum,TargetUser