Summing columns horizontally

  • I am trying to think of a way of adding the contents of certain numeric colmuns (about 7) and updating the results in another column

    However I want to do it more elegantly than

    Set Column8 = Column2 plus Column3 plus Column4 etc

  • What's wrong with this way?

    Frank

    http://www.insidesql.de

    http://www.familienzirkus.de

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • Maybe try a computed column?

    Tim Wilkinson

    "If it doesn't work in practice, you're using the wrong theory"
    - Immanuel Kant

  • OK, how do I do a computed column

  • You can create a computed column in a create or alter table statement.

    E.g.

    (Replace the word plus with a plus sign - its not showing when I preview )

    
    
    create table MY_TABLE
    (
    COL1 int,
    COL2 int,
    COL3 varchar(25),
    COL4_COMPUTED as col1 plus col2
    )

    OR

    
    
    alter table MY_TABLE
    add MY_COMPUTED_COL as EXISTING1 plus EXISTING2

    There is more info on computed columns in the CREATE TABLE & ALTER TABLE topics in SQL books online.

    Hope this helps!


    Cheers,
    mia

    Life moves pretty fast. If you don't stop and look around once in a while, you could miss it.

  • Thanks. Just what I needed

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

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