Benny
Benny

Reputation: 13

Can´t use GROUP BY after WHERE clause into subquery

I have this query:

left JOIN 
(SELECT * FROM ##TTA TT WHERE (TT.TAXCODE LIKE 'IVA0A' OR  TT.TAXCODE LIKE 'IVA0EXPT')) TT
GROUP BY TT.DATAAREAID, tt.VOUCHER, TT.MAINACCOUNTID 
AS TTIVA0A ON Asiento.Asiento = TTIVA0A.VOUCHER 
AND Asiento.Cuenta = TTIVA0A.AD
AND Asiento.Empresa = TTIVA0A.DATAAREAID

Problem is into GROUP BY clause I get:

Incorrect syntax near the keyword 'GROUP'.

Can someone explain me why I cant use GROUP BY after WHERE clause?

Upvotes: 0

Views: 159

Answers (1)

Sean Lange
Sean Lange

Reputation: 33571

If you were more careful with your formatting this would be painfully obvious.

Here is your code with the group by in the right place.

left JOIN 
(
    SELECT * 
    FROM ##TTA TT 
    WHERE TT.TAXCODE LIKE 'IVA0A' 
        OR  TT.TAXCODE LIKE 'IVA0EXPT'
    GROUP BY TT.DATAAREAID
        , TT.VOUCHER
        , TT.MAINACCOUNTID 
) AS TTIVA0A ON Asiento.Asiento = TTIVA0A.VOUCHER 
    AND Asiento.Cuenta = TTIVA0A.AD
    AND Asiento.Empresa = TTIVA0A.DATAAREAID

Upvotes: 2

Related Questions