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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jayraj ChhayaPosted Jan 16, 2024, 7:44 AM
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:
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.Naimish MakwanaPosted Jan 16, 2024, 5:23 AM
Please use below query.
Thanks
Jignesh KumarPosted Jan 16, 2024, 4:22 AM
Hello Ramco,
You can use this query to get the grand total as last row,
Prasad RaveendranPosted Jan 16, 2024, 3:51 AM
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:Prasad RaveendranPosted Jan 16, 2024, 3:48 AM
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:Prasad RaveendranPosted Jan 16, 2024, 3:46 AM
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: