sql server - How to list all Sub Groups in a column, using GROUP BY and COUNT in SQL -
i struggling displaying of sub-groups second column because have null values. want able show of rows regardless of value being null or not. want able display of content first column.
currently have:
dmd.type dmd.locstatus count() sum(dmd.balance) loc active 3 -70860.33 loc cancelled 1 0.00 locmtg active 2 -614268.02 locmtg approved 1 0.00 but want have:
dmd.type dmd.locstatus count() sum(dmd.balance) loc active 3 -70860.33 approved 0 0.00 cancelled 1 0.00 pending 0 0.00 rejected 0 0.00 locmtg active 2 -614268.02 approved 1 0.00 cancelled 0 0.00 pending 0 0.00 rejected 0 0.00 loc5k active 0 0.00 approved 0 0.00 cancelled 0 0.00 pending 0 0.00 rejected 0 0.00 my current code:
select dmd.type, dmd.locstatus, count(cust.no_) count, sum(dmd.balance) dmd inner join cust on dmd.rowno_custdmd_cust = cust.rowno (dmd.type 'loc%') group dmd.type, dmd.locstatus order dmd.type, dmd.locstatus is there way fix this?
to approach this, generate rows using cross join. use left join bring in rows values:
select t.type, l.locstatus, count(cust.no_) count, sum(dmd.balance) (select distinct dmd.type dmd dmd.type 'loc%') t cross join (select distinct dmd.locstatus dmd) l left join dmd on dmd.type = t.type , dmd.locstatus = l.locstatus left join cust on dmd.rowno_custdmd_cust = cust.rowno group t.type, l.locstatus order t.type, l.locstatus; as skipping values in column, should @ application layer, rather in database.
Comments
Post a Comment