how to sum some field in a row

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rsrinivasan
    New Member
    • Mar 2007
    • 221

    how to sum some field in a row

    Hi all,
    I am using SQLSERVER 2000.

    I have one table with the following structure,

    No Name H1 H2 H3 .. H100
    101 John 23 45 67 45
    102 Smith 22 56 58 56
    103 Greg 11 56 78 44

    I want to sum H1, H2, H3, H4...H100. How should i do it?

    Thanks in advance,
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Are you sure you have the most efficient design?
    A table with a hundred columns.
    You could add all the columns with a WHILE loop.
    Or could do something like H1+H2+H3+H4 ... AS totals
    Are you sure the DB structure should not be

    Code:
    members
    id    name
    101   John 
    102   Smith 
    103   Greg 
    
    scores
    id   value
    101    23
    101    45 
    101    67
    101    45
    102    22
    102    56

    Comment

    • Delerna
      Recognized Expert Top Contributor
      • Jan 2008
      • 1134

      #3
      Good design such as code greens turns complicated tasks into simple ones :)

      Also makes changing it from 100 numbers to 200 easier.

      Just insert the extra rows required

      instead of more columns and then changing all your insert and calculation queries to handle the extra columns.

      Comment

      Working...