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
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, June 24, 2014 7:49 AM
Points: 47, Visits: 139
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


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 10:47 AM
Points: 3,337, Visits: 7,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.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

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 Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Today @ 3:27 AM
Points: 90, Visits: 327
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