SUM and Group By

  • Hi all,

    I'm really struggling with a basic concept.

    I have the following data:

    ValueID RootID ProductID YearMonth ValueAmount

    1 1 1 200801 20

    2 1 2 200801 30

    3 1 1 200802 40

    4 1 2 200802 30

    5 2 1 200801 50

    6 2 2 200801 30

    I need to SUM the ValueAmount Per Root ID Per Month

    i.e for the above i need a result such as

    RootID YearMonth ValueAmount

    1 200801 50

    1 200802 70

    2 200801 80

    Any input on how to achieve this would be greatly appreciated..

  • Are you familiar with the group by clause and the aggregation functions in a sql statement?

    The aggregations (in this case SUM) get the columns you're trying to aggregate as a 'parameter'. So in this case if is SUM(ValueAmount)

    Do you follow that?

    The group by lists the columns that specify the groups. In this case, you want the total per RootID Per YearMonth, so you want to GROUP BY RootID, YearMonth.

    Does that help at all?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Thanks ! Exactly what i was looking for :hehe:

  • Pleasure.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 4 posts - 1 through 3 (of 3 total)

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