sql - How to sum all columns value of a table and display the total in new row by using Postgresql -
i using normal select query display rows
select type, debit, credit, (debit-credit) balance bank_cash_registers
now need display total additional row of postgresql query below image. how can achieve this?
another way using grouping sets. advantage can extended. furthermore think created purpose.
this should more efficient union solution data passed through once.
the following query returns want:
select coalesce(type, 'total: '), sum(debit), sum(credit), sum(debit - credit) balance bank_cash_registers group grouping sets ((type, debit, credit), ());
the following query groups values having same type (notice thing changed grouping sets clause):
select coalesce(type, 'total: '), sum(debit), sum(credit), sum(debit - credit) balance bank_cash_registers group grouping sets ((type), ());
result:
bank 0 1500 -1500 cash 0 700 -700 total: 0 2200 -2200
your updated question can solved way follows:
select case when grouping(debit) > 0 'total: ' else type end type, sum(debit), sum(credit), sum(debit - credit) balance bank_cash_registers group grouping sets ((type, debit, credit), (type));
you can add big total with
(...) grouping sets ((type, debit, credit), (type), ());
Comments
Post a Comment