sql server 2005 query syntax error
I have the following query and it gives me an error :"Incorrect syntax near the keyword 'GROUP'." <-shows me the second GROUP BY statement...Can you tell me why?
SELECTventId
,TargetUser
,TargetDomain
,String05
,Amount
,max开发者_JAVA技巧(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
If I create view for inner query it works fine(But I don't want to create a view because if my query is long-running query it gives me time-out).
You need to give an alias to your table:
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
I found the answeeeeeeeer
I should have written as tempTB
I mean my query should look like this:
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
精彩评论