Hi
In below query i want to group by code,name. But when i group then it gives error DueDate is not grouped by.
SQL
SELECT T1."CardCode", T1."CardName",
CASE
when (DAYS_BETWEEN(T0."DueDate", now()))+1 0 then -T0."SYSCred"
else T0."SYSDeb"
end
end "0-30 days",
FROM JDT1 T0 inner join OCRD T1 on T1."CardCode" = T0."ShortName"
INNER JOIN OJDT T2 ON T2."TransId"=T0."TransId"
WHERE T1."U_MSME" = 'Y'
AND "T1"."CardType" = 'S'
ORDER BY T1."CardCode", T0."TaxDate"Thanks

Jayraj ChhayaRank #304 · 8.2K reputationPosted Nov 7, 2024, 12:56 AM EST
Hello Ramco Ramco,
The error you are experiencing arises because SQL requires that all selected columns in a
GROUP BYquery must either be included in theGROUP BYclause or be aggregated. In your case, theDueDateis not included in theGROUP BY, which leads to the error.To resolve this, you can either include
DueDatein yourGROUP BYclause or apply an aggregate function to it. Here’s a modified version of your query that groups byCardCodeandCardName, while also using an aggregate function for theDueDate:SELECT T1."CardCode", T1."CardName", CASE WHEN (DAYS_BETWEEN(MAX(T0."DueDate"), NOW())) + 1 0 THEN -SUM(T0."SYSCred") ELSE SUM(T0."SYSDeb") END END AS "0-30 days" FROM JDT1 T0 INNER JOIN OCRD T1 ON T1."CardCode" = T0."ShortName" INNER JOIN OJDT T2 ON T2."TransId" = T0."TransId" WHERE T1."U_MSME" = 'Y' AND T1."CardType" = 'S' GROUP BY T1."CardCode", T1."CardName" ORDER BY T1."CardCode", MAX(T0."TaxDate");I used
MAX(T0."DueDate")to ensure that theDueDateis aggregated, thus resolving the error while maintaining the intended functionality of your query.Jignesh KumarRank #91 · 22.9K reputationPosted Nov 7, 2024, 12:07 AM EST
Hello Ramco,
You can use below query to do group by, You have not applied group by in your query.
SELECT T1."CardCode", T1."CardName", CASE WHEN (DAYS_BETWEEN(T0."DueDate", now())) + 1 0 THEN -T0."SYSCred" ELSE T0."SYSDeb" END END AS "0-30 days" FROM JDT1 T0 INNER JOIN OCRD T1 ON T1."CardCode" = T0."ShortName" INNER JOIN OJDT T2 ON T2."TransId" = T0."TransId" WHERE T1."U_MSME" = 'Y' AND T1."CardType" = 'S' GROUP BY T1."CardCode", T1."CardName", T0."DueDate" -- Grouping by DueDate ORDER BY T1."CardCode", T0."TaxDate";