Row wise sum based on condition

  • Hi All,

    I have an input like below.

    mytable: 

    id | name | no
    --------------
    1 | A | 10
    1 | A | 20
    1 | A | 40
    2 | B | 20
    2 | B | 20
    1 | C | 20
    1 | C | 23
    3 | D | 15
    3 | D | 15

    Need the output like below, how to achieve this , Sum of no based on id, name needs to result in Sumdat

    for eg

    id=1 and name =A and no we have 10+20+40=70 = Sumdat column

    id=3 and name =D and no we have 15+15=30 = Sumdat column

    mytable: 

    id | name | no | SumDat
    ----------------------
    1 | A | 10 | 70
    1 | A | 20 | 70
    1 | A | 40 | 70

    2 | B | 20 | 40
    2 | B | 20 | 40

    1 | C | 20 | 43
    1 | C | 23 | 43

    3 | D | 15 | 30
    3 | D | 15 | 30

    Thanks !

  • select id
    ,name
    ,no
    ,sum(no) OVER(PARTITION BY id, name)
    from mytable

Viewing 2 posts - 1 through 1 (of 1 total)

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