sql server - SQL Query - Group and SUM of Values -


i have following database structure:

id | payment (decimal) | paymentdate (datetime) | paymentstatus(int)

i able grouping of of payments on time year , date , total across payment status's using following query;

select  year = year(duedate), month = month(duedate), mmm = upper(left(datename(month,duedate),3)), totals = sum(payment)  paymentschedules duedate not null group  year(duedate), month(duedate), datename(month,duedate) order year, month 

this gives me results far good.

enter image description here

what able have added totals splits in each section. example if each payment paid (1) or unpaid (2) or overdue (3) not number of paid / unpaid / overdue total value of unpaid items / paid items / overdue items each year / month combination.

you need add sums case statements inside sum payments when correct status detected, this:

select year = year(duedate),        month = month(duedate),        mmm = upper(left(datename(month,duedate),3)),        totalpaid = sum(case when paymentstatus = 1 payment else 0 end),        totalunpaid = sum(case when paymentstatus = 2 payment else 0 end),        totaloverdue = sum(case when paymentstatus = 3 payment else 0 end),        totals = sum(payment) paymentschedules duedate not null group year(duedate),          month(duedate),          datename(month,duedate) order year,          month 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -