Hi
I have below query i want to display Grand Total in the end
select invoiceno , invoicedt , invoiceamt
from Sales
Thanks
Hi
I have below query i want to display Grand Total in the end
select invoiceno , invoicedt , invoiceamt
from Sales
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 16, 2024, 12:23 AM EST
Please use below query.
SELECT invoiceno, invoicedt, invoiceamt FROM Sales UNION ALL SELECT 'Total', NULL, SUM(invoiceamt) FROM Sales;Thanks
Prasad RaveendranRank #320 · 7.8K reputationPosted Jan 15, 2024, 10:46 PM EST
To display the Grand Total at the end of your query results, you can use the
UNIONorUNION ALLclause to combine the original query with an additional row containing the Grand Total. Here's an example:-- Your original query SELECT invoiceno, invoicedt, invoiceamt FROM Sales -- Union with Grand Total row UNION ALL -- Grand Total row SELECT NULL AS invoiceno, NULL AS invoicedt, SUM(invoiceamt) AS invoiceamt FROM Sales;Prasad RaveendranRank #320 · 7.8K reputationPosted Jan 15, 2024, 10:48 PM EST
An alternative approach is to use the
ROLLUPgrouping set. TheROLLUPoption is an extension to theGROUP BYclause and can be used to generate subtotals and grand totals for multiple levels of aggregation. Here's how you can modify your query to include a grand total usingROLLUP:SELECT CASE WHEN GROUPING(invoiceno) = 1 THEN 'Grand Total' ELSE invoiceno END AS invoiceno, MAX(invoicedt) AS invoicedt, SUM(invoiceamt) AS invoiceamt FROM Sales GROUP BY ROLLUP(invoiceno, invoicedt);Prasad RaveendranRank #320 · 7.8K reputationPosted Jan 15, 2024, 10:51 PM EST
To implement a Grand Total using the
OVERclause, you can use theSUM()window function along with thePARTITION BYclause to calculate the running total for each row. Then, you can use a conditional statement to identify the Grand Total row. Here's an example:SELECT invoiceno, invoicedt, CASE WHEN ROLLUP(invoiceno, invoicedt) IS NULL THEN SUM(invoiceamt) OVER () ELSE invoiceamt END AS invoiceamt FROM SalesJignesh KumarRank #91 · 22.9K reputationPosted Jan 15, 2024, 11:22 PM EST
Hello Ramco,
You can use this query to get the grand total as last row,
SELECT invoiceno, invoicedt, invoiceamt FROM Sales UNION ALL SELECT 'Grand Total' AS invoiceno, NULL AS invoicedt, SUM(invoiceamt) AS invoiceamt FROM Sales;Jayraj ChhayaRank #304 · 8.2K reputationPosted Jan 16, 2024, 2:44 AM EST
To display the grand total at the end of the SQL query result, you can use the
GROUP BYclause along with theWITH ROLLUPmodifier. This will allow you to calculate the total for each group and also include a row with the grand total.Here's an example query that demonstrates how to achieve this:
SELECT invoiceno, invoicedt, invoiceamt FROM Sales GROUP BY invoiceno, invoicedt, invoiceamt WITH ROLLUP;In the above query, the
GROUP BYclause is used to group the rows byinvoiceno,invoicedt, andinvoiceamt. TheWITH ROLLUPmodifier is added to include an additional row with the grand total.The result of this query will include all the rows from the
Salestable, grouped byinvoiceno,invoicedt, andinvoiceamt, along with a row at the end that displays the grand total.