Hi I am trying to add an additional col as a percentage so i have 3 columes as Account Balance, Current and Overdue
I need an additional col as a percentage (the calculation is Overdue*100/AccountBalance) but unsure of the syntax
sum(i.unall_amount) as 'Account Balance',
sum(case when (i.kind <> 'CSH') and (due_date >= getdate()) then i.unall_amount else 0 end) as 'Current',
sum(i.unall_amount)-sum(case when (i.kind <> 'CSH') and (due_date >= getdate()) then i.unall_amount else 0 end) as 'Overdue'
sum(i.unall_amount) as 'Account Balance',
sum(case when (i.kind <> 'CSH') and (due_date >= getdate()) then i.unall_amount else 0 end) as 'Current',
sum(i.unall_amount)-sum(case when (i.kind <> 'CSH') and (due_date >= getdate()) then i.unall_amount else 0 end) as 'Overdue',
sum(i.unall_amount)-sum(case when (i.kind <> 'CSH') and (due_date >= getdate()) then i.unall_amount else 0 end) * 100 / sum(i.unall_amount) As Percentage
Move your query to a CTE with the calculations - then in the outer query reference the columns:
WITH details
AS (
...
sum(i.unall_amount) as 'Account Balance',
sum(case when (i.kind <> 'CSH') and (due_date >= getdate()) then i.unall_amount else 0 end) as 'Current',
sum(i.unall_amount)-sum(case when (i.kind <> 'CSH') and (due_date >= getdate()) then i.unall_amount else 0 end) as 'Overdue'
...
)
SELECT ...,
OverDue * 100 / [Account Balance]
...
FROM details