Solved

SQL calculate a percentage

Posted on 2013-01-22
4
248 Views
Last Modified: 2013-01-22
In this query I'm trying to do a percent calcuation but for some reason anything less than 100% is not being calculated.

SELECT #tmp_ALLORDERS_COUNTS.TOTAL_ORDERS_COUNT, CAST(((#tmp_IVPROTOCOL_COUNTS.IVPROTOCOL_ORDERS_COUNT/#tmp_ALLORDERS_COUNTS.TOTAL_ORDERS_COUNT)*100) as decimal(18,2)) AS PERCENTAGE_USE
	, #tmp_IVPROTOCOL_COUNTS.* FROM  #tmp_ALLORDERS_COUNTS, #tmp_IVPROTOCOL_COUNTS
	WHERE  #tmp_ALLORDERS_COUNTS.DRUG_CODE = #tmp_IVPROTOCOL_COUNTS.DRUG_CODE

Open in new window


results look like this:
percentagecalcsql
0
Comment
Question by:ghettocounselor
  • 2
  • 2
4 Comments
 
LVL 34

Accepted Solution

by:
johnsone earned 500 total points
ID: 38807482
It is most likely an issue of data types.  I would cast each individual variable:

CAST(CAST(#tmp_IVPROTOCOL_COUNTS.IVPROTOCOL_ORDERS_COUNT as decimal(18,2))/CAST(#tmp_ALLORDERS_COUNTS.TOTAL_ORDERS_COUNT as decimal(18,2))*100.0) as decimal(18,2))

It is doing integer math and then doing the cast.  You need to do the cast first.
0
 

Author Comment

by:ghettocounselor
ID: 38807963
Needed to wrap with a few more parens but idea was sound. THANKS!

(CAST(CAST(#tmp_IVPROTOCOL_COUNTS.IVPROTOCOL_ORDERS_COUNT as decimal(18,2))/(CAST(#tmp_ALLORDERS_COUNTS.TOTAL_ORDERS_COUNT as decimal (18,2))) as decimal (18,2))*100) AS PERCENTAGE_USE
0
 

Author Closing Comment

by:ghettocounselor
ID: 38807966
As always appreciate quick response!
0
 
LVL 34

Expert Comment

by:johnsone
ID: 38808056
Sorry about the missing parens.  I didn't try it.  It looked like they were all there.  But, you got the idea.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now