Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# calculation in select

Posted on 2006-06-21
Medium Priority
1,439 Views
In sql server 2000 I have the following query in a stored procedure:
select  amount,
amount * VAT / 100 as vatsum,
amount + (amount * VAT / 100) as total
from table1

1. if the value of VAT is 0 then the value of total also become 0 although it is not 0 because it suppose to add the
amount. What is the problem here.
2. I wanted to write it like this
select        amount,
amount * VAT / 100 as vatsum,
amount + vatsum  as total
from table
but it is not accepted . what is the problem and how do I write it so that I write the calculation of vatsum only once?
Thankyou
Anat
0
Question by:ANAT2403

LVL 143

Expert Comment

ID: 16952040
select  amount,
amount * (VAT / 100) as vatsum,
amount + (amount * (VAT / 100)) as total
from table1
0

LVL 5

Expert Comment

ID: 16952086
Whether you write is as "amount + vatsum" or "amount + (amount...."  It would still calculate vatsum twice b/c vatsum is just an alias to that column.  SQL Server would need to calculate it twice.

you could re-write it to eliminate the multiple calculations.  Try the below query and see if it returns faster results.

select  amount,
amount * VAT / 100 as vatsum,
amount + (amount * VAT / 100) as total
from table1
where amount <> 0

UNION ALL select  0,
0,
0
from table1
WHERE amount = 0
0

LVL 5

Expert Comment

ID: 16952102
if amount is indexed you may return faster results this way.
0

Author Comment

ID: 16952533
nobody solved  the problem. the problem is not the fast of the query but that the total return 0 althogh in consists of
the amount which is not 0 and the vatsum which is 0
Can you help?
Anat
0

Author Comment

ID: 16952632
Ok  sorry, I found it. it's my mistake .
actually the VAT was written like this:
ISNULL(VAT, 0) and I didn't write it in the second part.
Sorry
Thankyou all
Anat
0

LVL 5

Accepted Solution

ID: 16952694
Are you certain of your problem? With a VAT of 0 there I would be hugely surpised if the total as zero (amount + (amount * 0 / 100)). Here's a hardcoded sample that shows the basic math works as expected:

declare @Amount decimal(8,2)
declare @Vat int

set @Amount = 10.23
set @Vat = 7

select @Amount, round(@Amount * @Vat / 100, 2), round(@Amount + (@Amount * @Vat / 100), 2)

set @Amount = 10.23
set @Vat = 0

select @Amount, round(@Amount * @Vat / 100, 2), round(@Amount + (@Amount * @Vat / 100), 2)

Are you perhaps not showing your full query? If you are not... have you considered how NULL will affect numeric calculations (SUM() and so forth)?

Regards,

Rob
0

Author Comment

ID: 16952767
Thankyou you are write as you can see in my previuos answer I didn't show the complete query and there was the
Anat
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
###### Suggested Courses
Course of the Month12 days, 14 hours left to enroll