Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Idiots calculation question

Author  Topic 

steamngn
Constraint Violating Yak Guru

306 Posts

Posted - 2004-12-31 : 09:54:09
I think I need a vacation...
Ok, We need to calculate the percentage of profit on sales items for commision. There is a rather lengthy sproc which is used here to do this, works fine. NOW the brass hats want it to show NEGATIVE percentages (loss) as well. This makes sense, but damned if I can remember how to change the formula. Here is the formula as it stands, which returns NULL if the price is 0.00:
SELECT
case when coalesce(prc,0)=0 then NULL
else ((cost/prc)*100) end
as pct
from ps_tkt_hist_lin

This of course returns NULL every time a saleman gives away an item (say a power cord for a dryer that was purchased), and prevents the dreaded 'divide by 0' error that would come up otherwise. We need to get the NULL values to show the true loss as a percentage.


There's never enough time to type code right,
but always enough time for a hotfix...

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2004-12-31 : 11:29:20
IF you give something away for free....then isn't the pct loss equal to 100...ie the lot!!
I think you need to categorise the prices received using the CASE clause...

prc > 0
prc = 0
prc < 0
prc = null

with appropriate calculations/formulae for each case.
Go to Top of Page

steamngn
Constraint Violating Yak Guru

306 Posts

Posted - 2004-12-31 : 12:12:03
quote:
IF you give something away for free....then isn't the pct loss equal to 100...ie the lot!!

Sure is, my friend! An update:
I took your advise, and I have the values I need working now.Now I have another question: Say the profit percentage is 25% on an item.
The returned value for profit percentage is .250000
How can I return the value as 25.0?
Thanks again,
Andy

There's never enough time to type code right,
but always enough time for a hotfix...
Go to Top of Page

AjarnMark
SQL Slashing Gunting Master

3246 Posts

Posted - 2004-12-31 : 12:20:28
quote:
Originally posted by steamngn
The returned value for profit percentage is .250000
How can I return the value as 25.0?

Multiply by 100?

---------------------------------------------------------------------------------
Infoneering: Information Technology solutions engineered to professional standards.
Go to Top of Page

steamngn
Constraint Violating Yak Guru

306 Posts

Posted - 2004-12-31 : 12:39:39
Ok,
I didn't elaborate enough:
select case
when coalesce(prc,0)=0 and coalesce(reg_prc,0)=0 and coalesce(cost,0)=0
then NULL
when coalesce(prc,0)=0 and coalesce(reg_prc,0)<>0 and coalesce(cost,0)<>0
then cast(-((reg_prc-cost)/cost)as numeric)
when coalesce (prc,0)<>0 and coalesce(cost,0)<>0
then cast((((prc-cost)/cost)*100) as numeric)
end
as pct,
case
when coalesce(prc,0)=0 and coalesce(reg_prc,0)=0 and coalesce(cost,0)=0
then NULL
when coalesce(prc,0)=0 and coalesce(reg_prc,0)<>0 and coalesce(cost,0)<>0
then cast(-((reg_prc-cost))as numeric)
when coalesce (prc,0)<>0 and coalesce(cost,0)<>0
then((prc-cost))
end
as profit_amt,
ps_tkt_hist_lin.cost,tkt_no,ps_tkt_hist_lin.item_no, im_item.categ_cod
from ps_tkt_hist_lin
join im_item
on ps_tkt_hist_lin.item_no=im_item.item_no
and im_item.item_no<>'NOTE'
and im_item.categ_cod<>'WALAB'
and im_item.categ_cod<>'RELAB'

This is the current code. This line:
then cast(-((reg_prc-cost)/cost)as numeric)

will return -1.00000, but if I multiply it by 100 then I get the ACTUAL calculation, say 149 or something rather than 100, for the percentage. It is the negative amounts that aren't calculating right..


There's never enough time to type code right,
but always enough time for a hotfix...
Go to Top of Page

steamngn
Constraint Violating Yak Guru

306 Posts

Posted - 2004-12-31 : 15:38:55
UPDATE!
I'm an idiot (no noise from the peanut gallery).
quote:
This is the current code. This line:

then cast(-((reg_prc-cost)/cost)as numeric)

will return -1.00000, but if I multiply it by 100 then I get the ACTUAL calculation, say 149 or something rather than 100, for the percentage. It is the negative amounts that aren't calculating right..


I was multiplying it like this:
then cast(-((reg_prc-cost)/cost)*100 as numeric)

when it needed to be like this:
then cast(-((reg_prc-cost)/cost)as numeric)*100

DUH!!
Thanks for the help!
Andy


There's never enough time to type code right,
but always enough time for a hotfix...
Go to Top of Page
   

- Advertisement -