How to sum values between multiple fields?

  • Suppose I have a table like the following:

         id       a     b     c     date

    ----------------------------------

      A123     0      1     2     03Oct06

      B123     0      3     2     03Oct06

      A123     0      1     5     03Oct06

      B123     0      10     6     03Oct06

     

    What I want to do is sum the a, b, c field so that I can get the following table:

         id       total     date

    ----------------------------------

      A123       9       03Oct06

      B123       21      03Oct06

    How can i do this?

  • select id,'total'=a+b+c,date from table_name

  • It works but this is not a result I want. I think the query should use group by function.

  • you are right.. for the correct result there should be a group by clause on the Id and Date.

    Cheers

    cheers

  • The solution for such problems is obtained by using the aggreggate functions.

    The required results can be obtainted by writing the query as below:

     

    select id,total=sum(a+b,c)  from table group by id

     


    Kindest Regards,

    Sureshkumar Ramakrishnan

  • select id, sum (a) + sum(b)+ sum (c) as d from x group by id

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

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