Solved

SQL calculate a percentage

Posted on 2013-01-22
4
256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 35

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 35

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses

623 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