Sum float datatype column

  • 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

  • What do you mean by "I'm unable to sum the cost."? SUM can be applied to float.

    As float are not exact datatype it is a bad choice for such calculations and keeping monetary values.

    If all of values in your float column represent a whole numbers within int range, converting it to INT should not cause lose of data. However, it's very rare (otherwise why it is a float in first case?). If you have values with decimals you will be better to convert them to DECIMAL datatype.

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • I changed the datatype to int. But now when I run the query It says invalid column name for cost.

    Table Schema:

    CREATE TABLE [dbo].[Application](

    [ID] [int] NOT NULL,

    [Date] [date] NULL,

    [ Cost] [int] NULL,

    [Funding] [varchar](5) NOT NULL

    ) ON [PRIMARY]

    GO

  • Looks like you have a space at front of the Cost, so yuor full column name is [ Cost]. Rename it to remove the space

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • I am using the below query and doesn't work

    SELECT LTRIM(Cost) FROM Application

  • You are trying to TRIM the value in the column which doesn't exist!

    You or someone else was not very accurate while changing table definition and added space to the name of the column. Check what you have posted previously:

    CREATE TABLE [dbo].[Application](

    [ID] [int] NOT NULL,

    [Date] [date] NULL,

    [ Cost] [int] NULL,

    [Funding] [varchar](5) NOT NULL

    ) ON [PRIMARY]

    can you see a space? [ Cost] should be changed to [Cost]

    Otherwise, you always need to use square brakets when referring to this column. In your case the following will work:

    SELECT [ Cost] FROM dbo.Application

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • Yeah it worked. Thanks

Viewing 7 posts - 1 through 6 (of 6 total)

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