Thursday, March 29, 2012

Formula for calculated column

Hi,
I'm struggling to get a calculated column to work in sql, the fields to be calculated are:
[AdRevenue_a] money
[Admissions_a] int
[DoorPrice_a] smallmoney
[DoorSplit_a] money
And the calculation I require is:
(AdRevenue_a / ( (Admissions_a * DoorPrice_a) - DoorSplit_a )) * 100
This is what I think it should be but it doesn't work...
convert(decimal(6,2), ((AdRevenue_a / ((Admissions_a * DoorPrice_a) - DoorSplit_a))*100) ))

Any suggestions??

(AdRevenue_a / ( (Admissions_a * DoorPrice_a) - DoorSplit_a )) * 100
should work.
In this:
convert(decimal(6,2), ((AdRevenue_a / ((Admissions_a * DoorPrice_a) - DoorSplit_a))*100) ))
you have an extra bracket at the end and it will work if you remove it.|||That didn't fix the issue.
The problematic field seems to be DoorSplit_a, if this is removed the rest of the calculation works??
|||Do you have any sample data that you can provide. Also are any of the columns nullable? IF so you might need to use ISNULL() appropriately.
If I supplied all the values properly it seemed to work:

DECLARE
@.AdRevenue_amoney,
@.Admissions_aint
,@.DoorPrice_asmallmoney
,@.DoorSplit_amoney

select
@.AdRevenue_a= 100
,@.Admissions_a= 50
,@.DoorPrice_a= 2
,@.DoorSplit_a= 25

select(@.AdRevenue_a/((@.Admissions_a* @.DoorPrice_a)- @.DoorSplit_a))* 100

selectconvert(decimal(6,2),((@.AdRevenue_a/((@.Admissions_a* @.DoorPrice_a)- @.DoorSplit_a))*100))

|||All aggregate functions in SQL Server ignore NULL values except COUNT (*), and the ISNULL function will replace NULL with 0 which could give you wrong numbers if [DoorSplit_a] allows NULL. Try the link below for more info. Hope this helps
http://www.akadia.com/services/dealing_with_null_values.html|||Thanks for the replies.
I'm not even getting as far as the data.
The error is thrown by sql when I try to enter the calculation into the formula box.|||Please post the exact formula you are now trying to enter. As Dinakar pointed out, yourfirst example had a mistmatched number of opening and closingparentheses.
Also, for future questions, please use a more specific term than "doesnot work". An exact description of the error you are encounteringwill go a long way towards pinpointing and correcting your problem.

No comments:

Post a Comment