locked
invalid column name sum RRS feed

  • Question

  • User639567535 posted

    i treid this query 

    select sum[amount ist] as Amount , id, category from abctable 

    where category in [ 'a', 'b', 'c' ] 

    group by id , category 

    now this an error 

    invalid column name SUM 

    what should  i do 

    Tuesday, January 29, 2019 9:24 AM

All replies

  • User364663285 posted

    Try

    select sum([amount ist]) as Amount , id, category from abctable 
    where category in('a', 'b', 'c') 
    group by id , category 

    Tuesday, January 29, 2019 10:01 AM
  • User639567535 posted

    select sum([amount ist]) as Amount , id, category from abctable 
    where category in('a', 'b', 'c') 
    group by id , category 

    i do this .. this shows an error 

    "operand data type varchar is invalid for sum operator"

    Tuesday, March 12, 2019 6:31 AM
  • User77042963 posted
    select sum(try_cast([amount ist] as decimal(10,2)) )as Amount , id, category 
    from abctable 
    where category in ( 'a', 'b', 'c' ) 
    group by id , category 

    Tuesday, March 12, 2019 4:57 PM
  • User753101303 posted

    Hi,

    Or consider changing your column type from varchar to decimal(10,2) or whatever is wide enough for you. Using  a varchar column to store numeric values seems a poor design. If you have a currency symbol, the currency information should be likely stored in its own column (so that you can handle conversion when doing your sum for example).

    Tuesday, March 12, 2019 5:11 PM