Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Sum a float column in SQL Expand / Collapse
Author
Message
Posted Wednesday, February 12, 2014 4:42 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, October 8, 2014 7:18 AM
Points: 55, Visits: 184
Hi,

I'm using this query to sum the values. The cost column is a float datatype and what could I differently do here to sum the cost. I'm unable to sum the cost.

Also is there any way I change the datatype to int for Cost column without losing the data.

select ID, MAX(Date) Date, SUM(Cost) Cost, MAX(Funding) Funding from Appllication
group by ID
Post #1540640
Posted Wednesday, February 12, 2014 9:51 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 11:27 AM
Points: 4,046, Visits: 9,201
Why can't you sum a float column? float is a numeric data type and it can be used in SUM().
Here's what I used to test.
DECLARE @Appllication TABLE(
ID int,
Date date,
Cost float,
Funding float
)
INSERT INTO @Appllication
VALUES
(1,'20140201', 10.52, 5),
(1,'20140202', 20, 15),
(3,'20140205', 30.1245, 50),
(3,'20140201', 110, 5)
select ID, MAX(Date) Date, SUM(Cost) Cost, MAX(Funding) Funding from @Appllication
group by ID




Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1540874
Posted Wednesday, February 12, 2014 10:05 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, December 16, 2014 12:52 PM
Points: 111, Visits: 343
i would mention something here,
be cautious when you work with real number, because there is a little bit difference in result when you sum float, decimal or money data type.
In the case of precision, you have to rely on a existing result and to work with appropriate data type
Post #1540883
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse