Microsoft
Software
Hardware
Network
Question : How I can simplify a series of nested case statements in a MS SQL 2005 query
Hello,
I'm working on an average debt oustanding query and I have my debt principal balances split into months (columns) and debt type (rows). See below a section of my code used to calculate a June year-to-date average outstanding balance. If the debt has been open for the entire period then a simple average is used in my formula. However if the debt is incurred sometime in the middle of the period I need to adjust the denominator in my formula to be the number of periods open. I'm trying to avoid a series of nested CASE statements as I have to do the calculation for each of the twelve months. Any Ideas?
See my image attachment below. The first line should be divided by 2 instead of 6 as the debt was only open for two months at the time of the report.
select
Category
Beg_bal,
Jan_bal,
Feb_bal,
Mar_bal,
Apr_bal,
May_bal,
(Beg_bal + Jan_bal + Feb_bal + Mar_bal + Apr_bal + May_bal) / 6 as ytd_Jun_avg,
from #working
Thanks!
David
average-example.JPG
(28 KB)
(File Type Details)
average example
Answer : How I can simplify a series of nested case statements in a MS SQL 2005 query
use sign function
(Beg_bal + Jan_bal + Feb_bal + Mar_bal + Apr_bal + May_bal)
/ (sign(Beg_bal) + sign(Jan_bal) + sign(Feb_bal) + sign(Mar_bal) + sign(Apr_bal) + sign(May_bal))
as ytd_Jun_avg,
Random Solutions
Open file Security Message
Looking for IP and Mac addresses for all devices on network
XP refuses to boot after OS boot order changes were made using EasyBCD
Screen Saver Group Policy make different for user groups
get wrong autodiscover server in warning message....
Importing Custom Exchange Public Folder Data into Access 2007 table
How to use the processing core library to create a PIMAGE in Java Netbeans?
how to change a textbox from a combobox
an error every 5 minutes on exchange 2007 Event ID 10016
Does ODBC connection from INFORMIX DB to MS ACCESS has committed read on the data?