Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Getting the SUM of two field values [modified]

Getting the SUM of two field values [modified]

Scheduled Pinned Locked Moved Database
help
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nino_1
    wrote on last edited by
    #1

    Hi, I'm trying to get the sum of two fields: declare @totalsum money set @totalsum = (sum(fieldname1 + fieldname2)) the error message displays an invalid fieldname for both fields, i have the fieldnames spelled correctly but still get the error. Any suggestions would be great. Thanks, Nino Italy == World Champions -- modified at 15:06 Monday 10th July, 2006

    E 1 Reply Last reply
    0
    • N Nino_1

      Hi, I'm trying to get the sum of two fields: declare @totalsum money set @totalsum = (sum(fieldname1 + fieldname2)) the error message displays an invalid fieldname for both fields, i have the fieldnames spelled correctly but still get the error. Any suggestions would be great. Thanks, Nino Italy == World Champions -- modified at 15:06 Monday 10th July, 2006

      E Offline
      E Offline
      Eric Dahlvang
      wrote on last edited by
      #2

      I'm not sure what you are trying to do, but if you want the sum of all (fieldname1 + fieldname2) values for every record in a particular table, then you need to supply the table name and change the word set to select.

      declare @totalsum money

      select @totalsum = (sum(fieldname1 + fieldname2)) from tablename

      But most likely, you want the sum of these two fields for a particular row, so you would need a where clause:

      declare @totalsum money

      select @totalsum = fieldname1 + fieldname2 from tablename where primarykeyfield = 1

      or something... --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

      N 1 Reply Last reply
      0
      • E Eric Dahlvang

        I'm not sure what you are trying to do, but if you want the sum of all (fieldname1 + fieldname2) values for every record in a particular table, then you need to supply the table name and change the word set to select.

        declare @totalsum money

        select @totalsum = (sum(fieldname1 + fieldname2)) from tablename

        But most likely, you want the sum of these two fields for a particular row, so you would need a where clause:

        declare @totalsum money

        select @totalsum = fieldname1 + fieldname2 from tablename where primarykeyfield = 1

        or something... --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

        N Offline
        N Offline
        Nino_1
        wrote on last edited by
        #3

        Ok, I should have posted the entire code block, I tried the method listed above with the rest of the SELECT statement and get an error message as follows. Server: Msg 141, Level 15, State 1, Line 4 A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations. Here is the entire code block:

        declare @totalsum money

        select @totalsum = (sum(d.amount + d.debit)), a.officer, d.amount,d.debit, e.fullesc as ESCROW_AND_CITY_CODE, e.open_date, e.close_date, e.ftype, a.street, a.city, a.state, a.zip
        from escrow e inner join a10 a on e.escrow = a.escrow inner join e120 d on d.escrow = e.escrow and a.officer is not null order by e.open_date

        Thanks Nino

        E 1 Reply Last reply
        0
        • N Nino_1

          Ok, I should have posted the entire code block, I tried the method listed above with the rest of the SELECT statement and get an error message as follows. Server: Msg 141, Level 15, State 1, Line 4 A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations. Here is the entire code block:

          declare @totalsum money

          select @totalsum = (sum(d.amount + d.debit)), a.officer, d.amount,d.debit, e.fullesc as ESCROW_AND_CITY_CODE, e.open_date, e.close_date, e.ftype, a.street, a.city, a.state, a.zip
          from escrow e inner join a10 a on e.escrow = a.escrow inner join e120 d on d.escrow = e.escrow and a.officer is not null order by e.open_date

          Thanks Nino

          E Offline
          E Offline
          Eric Dahlvang
          wrote on last edited by
          #4

          Yeah, you can't do that. If you want to sum all amount and debit columns for the entire table...then you wouldn't also pull back all the other fields. If you want to just add amount and debit together for each record, then you don't need a sum aggregate function.

          select d.amount + d.debit as recsum, a.officer, d.amount,d.debit,
          e.fullesc as ESCROW_AND_CITY_CODE, e.open_date, e.close_date, e.ftype,
          a.street, a.city, a.state, a.zip
          from escrow e inner join a10 a on e.escrow = a.escrow
          inner join e120 d on d.escrow = e.escrow and a.officer is not null
          order by e.open_date

          declare @totalsum money
          select @totalsum = sum(d.amount + d.debit)
          from escrow e inner join a10 a on e.escrow = a.escrow
          inner join e120 d on d.escrow = e.escrow and a.officer is not null

          or you could combine them like this - and every record will have a column with the TotalSum in it:

          select (select sum(d2.amount + d2.debit)
          from escrow e2 inner join a10 a2 on e2.escrow = a2.escrow
          inner join e120 d2 on d2.escrow = e2.escrow and a2.officer is not null) as totalsum,
          d.amount + d.debit as recsum, a.officer, d.amount,d.debit,
          e.fullesc as ESCROW_AND_CITY_CODE, e.open_date, e.close_date, e.ftype,
          a.street, a.city, a.state, a.zip
          from escrow e inner join a10 a on e.escrow = a.escrow
          inner join e120 d on d.escrow = e.escrow and a.officer is not null
          order by e.open_date

          --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

          N 1 Reply Last reply
          0
          • E Eric Dahlvang

            Yeah, you can't do that. If you want to sum all amount and debit columns for the entire table...then you wouldn't also pull back all the other fields. If you want to just add amount and debit together for each record, then you don't need a sum aggregate function.

            select d.amount + d.debit as recsum, a.officer, d.amount,d.debit,
            e.fullesc as ESCROW_AND_CITY_CODE, e.open_date, e.close_date, e.ftype,
            a.street, a.city, a.state, a.zip
            from escrow e inner join a10 a on e.escrow = a.escrow
            inner join e120 d on d.escrow = e.escrow and a.officer is not null
            order by e.open_date

            declare @totalsum money
            select @totalsum = sum(d.amount + d.debit)
            from escrow e inner join a10 a on e.escrow = a.escrow
            inner join e120 d on d.escrow = e.escrow and a.officer is not null

            or you could combine them like this - and every record will have a column with the TotalSum in it:

            select (select sum(d2.amount + d2.debit)
            from escrow e2 inner join a10 a2 on e2.escrow = a2.escrow
            inner join e120 d2 on d2.escrow = e2.escrow and a2.officer is not null) as totalsum,
            d.amount + d.debit as recsum, a.officer, d.amount,d.debit,
            e.fullesc as ESCROW_AND_CITY_CODE, e.open_date, e.close_date, e.ftype,
            a.street, a.city, a.state, a.zip
            from escrow e inner join a10 a on e.escrow = a.escrow
            inner join e120 d on d.escrow = e.escrow and a.officer is not null
            order by e.open_date

            --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

            N Offline
            N Offline
            Nino_1
            wrote on last edited by
            #5

            Great! thanks so much. Nino

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups