Hi
I am getting error T1."CreatedBy" must be in a Group clause. How it can be used without adding in Group By
SELECT T0."TransId",T1."TransType", T1."Account",Max(T2."AcctName"),Sum(T1."Debit"), Sum(T1."Credit"),
CASE
WHEN T1."TransType" = '13' THEN (Select max(A0."LocCode") from INV1 A0 where A0."DocEntry" = T1."CreatedBy")
ELSE ' ****** '
END AS "Location"
Thanks
Muhammad Imran AnsariPosted Jan 24, 2025, 7:14 AM
Hi Ramco,
The error occurs because T1."CreatedBy" is being used in the CASE statement without being included in the GROUP BY clause. In SQL, when you use aggregate functions (e.g., SUM, MAX) and non-aggregated columns in the same query, all non-aggregated columns must either be included in the GROUP BY clause or be part of an aggregate function.
If you want to use T1."CreatedBy" in the CASE statement without adding it to the GROUP BY clause, you can achieve this by modifying the query. Here's how you can handle it:
1. Using Subquery Method:
2. Add "CreatedBy" to the GROUP BY Clause:
You can use either any method at your convenience.
Thank you.
Tuhin PaulPosted Jan 24, 2025, 12:37 PM
Part -2
Let me explain the data processing for this query. as how things are working:
Tuhin PaulPosted Jan 24, 2025, 12:32 PM
Part -1
The error "T1.'CreatedBy' must be in a Group BY" occurs because you're using an aggregate function (Max, Sum) along with a column that is not part of the GROUP BY clause. In SQL, when you use aggregate functions, any column not inside an aggregate function must be included in the GROUP BY clause.
Key modifications:
T1."CreatedBy"to the GROUP BY clauseThe problem arises because:
CreatedBycolumn is not part of an aggregate function, so it needs to be in the GROUP BYA mental model to understand this: