Hi
I have below query . I want to display sum of the last 2 columns at the Bottom.
SELECT T3."ShortName" AS "Vendor Code",(Select Max("CardName") from OCRD where "CardCode" = T3."ShortName") AS "Vendor Name",T3."Debit", T3."Credit"
FROM OJDT T2 INNER JOIN JDT1 T3 ON T2."TransId" = T3."TransId" WHERE
(T3."ShortName"=:Vendor )
UNION ALL
SELECT T2."CardCode" AS "Vendor Code",T2."CardName" AS "Vendor Name",(T2."DocTotal") + (T2."DpmAmnt") + (T2."DpmVat"),0
FROM OINV T2
WHERE (T2."CardCode"=:Vendor )
Thanks
Naveen KumarPosted Dec 26, 2024, 11:25 AM
Please check the below approach:
Jayraj ChhayaPosted Dec 26, 2024, 9:48 AM
Hello Ramco Ramco,
To achieve the desired outcome of displaying the sum of the last two columns at the bottom of your SQL query results, you can utilize a
UNION ALLto append a summary row. Here’s how you can modify your existing query:In this modified query, the last
UNION ALLstatement calculates the total sums of the "Debit" and "Credit" columns and labels the row as "Total". This approach ensures that the summary appears at the bottom of your result set, providing a clear overview of the financial data.Jignesh KumarPosted Dec 26, 2024, 8:39 AM
Hello Ramco,
please use below query to get desire out put,
Jaish MathewsPosted Dec 26, 2024, 8:35 AM
To display the sum of the last two columns (
DebitandCredit) in your query, you can add a calculated column to perform the addition. The SQL syntax for the SUM calculation will depend on the database you're using (e.g., SAP HANA, SQL Server, etc.). Here's the adjusted query:Updated Query:
Explanation of Changes:
New Column
Total:Consistency Across UNION:
UNION ALLoperation requires all SELECT statements to have the same number of columns and compatible data types.Naming Conventions:
Totalcolumn is added to display the sum ofDebitandCreditin the first part and to maintain compatibility in the second part.This query now calculates and displays the sum of the last two columns as a new column labeled Total.