Hi
I am getting error T1."CreatedBy" must be in a Group clause. How it can be used without adding in Group By
SQL
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 AnsariRank #326 · 7.6K reputationPosted Jan 24, 2025, 2:14 AM EST
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:
SELECT T0."TransId", T1."TransType", T1."Account", MAX(T2."AcctName") AS "AcctName", SUM(T1."Debit") AS "TotalDebit", SUM(T1."Credit") AS "TotalCredit", CASE WHEN T1."TransType" = '13' THEN ( SELECT MAX(A0."LocCode") FROM INV1 A0 WHERE A0."DocEntry" = T1."CreatedBy" ) ELSE ' ****** ' END AS "Location" FROM Transactions T0 JOIN Accounts T1 ON T0."TransId" = T1."TransId" JOIN AccountDetails T2 ON T1."Account" = T2."AccountId" GROUP BY T0."TransId", T1."TransType", T1."Account";2. Add "CreatedBy" to the GROUP BY Clause:
SELECT T0."TransId", T1."TransType", T1."Account", MAX(T2."AcctName") AS "AcctName", SUM(T1."Debit") AS "TotalDebit", SUM(T1."Credit") AS "TotalCredit", CASE WHEN T1."TransType" = '13' THEN ( SELECT MAX(A0."LocCode") FROM INV1 A0 WHERE A0."DocEntry" = T1."CreatedBy" ) ELSE ' ****** ' END AS "Location" FROM Transactions T0 JOIN Accounts T1 ON T0."TransId" = T1."TransId" JOIN AccountDetails T2 ON T1."Account" = T2."AccountId" GROUP BY T0."TransId", T1."TransType", T1."Account", T1."CreatedBy";You can use either any method at your convenience.
Thank you.
Tuhin PaulRank #7 · 71.9K reputationPosted Jan 24, 2025, 7:32 AM EST
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.
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" FROM YourMainTable T0 JOIN YourTransactionTable T1 ON [appropriate join condition] JOIN YourAccountTable T2 ON [appropriate join condition] GROUP BY T0."TransId", T1."TransType", T1."Account", T1."CreatedBy" -- Add this to resolve the GROUP BY errorKey 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:
Tuhin PaulRank #7 · 71.9K reputationPosted Jan 24, 2025, 7:37 AM EST
Part -2
Let me explain the data processing for this query. as how things are working: