Little help required with T-SQL

  • Hiya

    I've gotta add up the values of X as a total for each of the ID for all years its just that I'm not sure how too, the table looks like.

    ID Yr X

    213199230589552800

    213199330932285100

    213199430103017800

    217199813103786000

    217199911357626000

    217200011223290800

    I was playing with sum but that only sees to add up X and not be able to split it up into ID eg

    ID Total Value

    213

    217

    Cheers 🙂

    Remember
    Without Change something sleeps inside of us that seldom awakens, the sleeper must awaken!!

  • Use the Group BY clause

    -- sum of each ID for all yrs

    Select ID,SUM(X) as Total from table Group BY ID

    -- sum of each ID yr wise

    Select ID,SUM(X) as Total from table Group BY ID,Yr

    "Keep Trying"

  • I cant understand your expectation,

    anyhow use this example

    select id,sum(x) from table_name group by id

  • Cool thanks, thou I've just found out that views can not contain aggregates functions or Group by 🙁

    So looks like it back to the drawing board for me.

    Thanks for all your help 🙂

    Remember
    Without Change something sleeps inside of us that seldom awakens, the sleeper must awaken!!

  • Mr J (3/4/2009)...I've just found out that views can not contain aggregates functions or Group by ...

    That is not true; they can have both.

  • You can use group by in views. Check out the example in BOL. It contains group by.

    "Keep Trying"

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

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