Skip to content
Advertisement

adding summary row to the end of the table

Hi I have a table with 4 columns. the table is as below

sampleId    totalAmount discount    netAmount
      1         120        40           80
      2         200        50           150
      3         400        100          300

Now i want the totals summary row at the bottom of the table. Please look at the image file below. how can i achieve this?

enter image description here

Advertisement

Answer

You can use UNION ALL as below

select cast(sampleId as char(10)) as sampleId, totalAmount,discount, netAmount
from tab
union all
select 'Total', sum(totalAmount),sum(discount), sum(netAmount)
from tab

SqlFiddle Demo

1st column is converted to varchar becouse you want to Total word atthe bottom. Columns types in UNION must be the same type.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement