sql server 2005查询语法错误

时间:2010-11-08 13:46:31

标签: sql-server-2005

我有以下查询,它给了我一个错误:“关键字'GROUP'附近的语法不正确。” < - 显示第二个GROUP BY语句......你能告诉我为什么吗?

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,Amount // ERROR SHOWS HERE

ORDER BY maximum,TargetUser

如果我为内部查询创建视图,它工作正常(但我不想创建视图,因为如果我的查询是长时间运行的查询,它会让我超时)。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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