Reputation: 56
SQL keeps rounding the results of my calculations and I do not seem to find an article on how to fix this.
I am trying to do calculations within SQL. This to BI self service easier.
SELECT
COUNT(CONTRACTID) / (SELECT COUNT(RENTALOBJECTID) FROM PMCCONTRACTOBJECT) AS result
FROM PMCCONTRACT
The result I am getting is 3 while I expect to get 3.2973.
Wow sorry, i added the wrong code. Should make more sense now
Upvotes: 0
Views: 613
Reputation: 56
Well i gave little information i was able to find the awnser
SELECT cast (count(CONTRACTID) as decimal) / (SELECT count(RENTALOBJECTID)
FROM PMCCONTRACTOBJECT) AS result FROM PMCCONTRACT
Upvotes: 0
Reputation: 3701
since you are getting 3, you possibly allowed an integer division when you didn't mean to - always cast to float or suitable Decimal before dividing - or use suitable data types in your calculation
see the following example as an illustration of what happens
select 10/3, cast(10 as float) / cast(3 as float)
Upvotes: 1