Solved

basic maths division in sql stored procedure

Posted on 2011-02-15
4
789 Views
Last Modified: 2012-05-11
Hi experts,

See in the code that I have a stored proc that successfully sets @totalOrderCount to the correct value in the first block but when used in the second select statement as part of a math column, it fails.  I've proven that both the elements in this maths column are working by putting them in as sepratae columns in their own right, but the actual maths column of: "(COUNT(o.orders_id) / @totalOrderCount) AS orderPC" just returns zeroes.

132      0      0      957
189      1      0      957
54      2      0      957
131      3      0      957
120      4      0      957
84      5      0      957
87      6      0      957
92      7      0      957
22      8      0      957
26      9      0      957
18      10      0      957
2      11      0      957

Confused... ?
DECLARE @totalOrderCount INT

BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.

	
	--SET NOCOUNT ON;
	
	
	SET @totalOrderCount = (SELECT COUNT(o.orders_id)
	FROM orders O 
	WHERE o.orders_status = 4 
	AND DATEDIFF(DAY, o.date_purchased, GETDATE()) < 31)

SELECT COUNT(o.orders_id) AS ordercount, (DATEDIFF(DAY,  o.date_purchased, osh.orders_status_history_timestamp)) AS timetaken,
(COUNT(o.orders_id) / @totalOrderCount) AS orderPC, @totalOrderCount AS orderTotal
FROM orders O, dbo.tblPS_orders_status_history OSH WHERE
o.orders_status = 4

Open in new window

0
Comment
Question by:jammy-d0dger
  • 2
4 Comments
 

Author Comment

by:jammy-d0dger
ID: 34896531
sorry. I should add that I have left some of the where clause out of the stored proc as it's not relevant.
0
 
LVL 18

Accepted Solution

by:
deighton earned 300 total points
ID: 34896635
cast as float prior to division, and multiply by 100 perhaps to get percentage

SELECT COUNT(o.orders_id) AS ordercount, (DATEDIFF(DAY,  o.date_purchased, osh.orders_status_history_timestamp)) AS timetaken,
(cast(COUNT(o.orders_id) as float) / cast(@totalOrderCount as float)) AS orderPC, @totalOrderCount AS orderTotal
FROM orders O, dbo.tblPS_orders_status_history OSH WHERE
o.orders_status = 4

Open in new window

0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 200 total points
ID: 34897467
i guess you are doing intefer arithmetic and losing precision..

either convert the operand to a more  precise data type or start off by multiplying the top
by 1.00

(1.00*count(o.orders_id))/@totalordercount
0
 

Author Closing Comment

by:jammy-d0dger
ID: 34898180
Thanks guys, both solutions/approaches work perfectly.  I've awarded slightly more to the quickest answer.  Many thanks to you both.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
LTrim & Double Space Correction 5 42
SQL Insert to Begin if data exists 2 33
SQL Log size 3 20
Microsoft CRM 4.0 26 22
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

820 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