selectp.partnerid,sum(casewhenc.amountisnotnullthenc.amountelse0end)asamount,sum(casewhenc.netamtisnotnullthenc.netamtelse0end)astotal,sum(casewhen(c.netamt-d.paidamount)isnotnullthen(c.netamt-d.paidamount)else0end)asremainingamount,sum(casewhend.paidamountisnotnullthend.paidamountelse0end)aspai