Sum a float column in SQL

  • 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

  • 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(

    IDint,

    Datedate,

    Costfloat,

    Fundingfloat

    )

    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.
    General Disclaimer:
    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?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • 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

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply