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 

its displayed below image enter image description here

now need display total additional row of postgresql query below image. how can achieve this?

enter image description here

and there option separate total based on type below.. enter image description here

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

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -