Solved

basic maths division in sql stored procedure

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

717 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