Solved

basic maths division in sql stored procedure

Posted on 2011-02-15
4
774 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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.

777 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