Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Round the Floating values

Author  Topic 

archana23
Yak Posting Veteran

89 Posts

Posted - 2012-05-30 : 15:10:36
Hello,

I am new to sql queries in my query i am calculating Average by using

select Sum(b.items)/COUNT(Distinct a.ServiceDateTime) as Avg

For example if answer is 5.8 above expression returning 5 but i want to show 6.

Like 5.1 -- need to show 5
5.7 -- need to show 6

Can you help me on this please

Thanks

Archana

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-05-30 : 15:26:03
Take a look at the ROUND function.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-30 : 15:41:11
casting it to int also will provide you with required result. ROUND will still preserve the decimal part

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-05-31 : 09:23:52
[code]
select convert(int, Sum(b.items) * 1.0 / COUNT(Distinct a.ServiceDateTime)) as Avg
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

archana23
Yak Posting Veteran

89 Posts

Posted - 2012-05-31 : 09:41:01
Hello Khtan,

Thank you for your reply.

Actually my requirement is to round the values to int as

5.1 to 5.5 -- as 5
5.6 to 5.9 -- as 6

but above expression is converting 5.7 to 5 which is not correct.

Can you please help me on this..

Thank you.

Archana
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-05-31 : 09:47:33
then round( < value > , 0) it first before convert


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-05-31 : 13:17:10
[code]DECLARE @Foo FLOAT = 5.6667;

SELECT ROUND(@Foo, 0)[/code]Works jsut fine. Can you show some sample data and expected output? It looks like your formual has issues, meaning you are doing INTeger math and not decimal math or your are rounding the the wrong palce.

EDIT: Quck guess: [code]Round((Sum(b.items) * 1.0) / COUNT(Distinct a.ServiceDateTime),0)[/code]
Go to Top of Page

archana23
Yak Posting Veteran

89 Posts

Posted - 2012-05-31 : 14:58:52
Hello Lamprey,

Thank you its working now..

Archana
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-31 : 15:22:30
quote:
Originally posted by archana23

Hello Lamprey,

Thank you its working now..

Archana


see this to understand the reason

http://beyondrelational.com/modules/2/blogs/70/posts/10825/beware-of-implicit-conversions.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -