none
TSQL how can I sum values except absolutes RRS feed

  • Question

  • I would like to sum values in my table, except the ones that are absolute (field absolute, value = 1). If that's the case, the summing should reset. Example:

    Date              Value    Absolute

    1-1-2020        4          0

    1-2-2020        7          1

    1-3-2020        3          0

    Regular SUM() would return (4+7+3=) 14. But in this example it should reset at value 7, which makes a sum of (7+3=) 10.

    How can I make this work?

    Saturday, November 7, 2020 5:22 PM

All replies

  • Hi,

    please try:

    drop table foo
    create table foo(value int , absolute int)
    truncate table foo
    insert into foo(value,absolute) values (4,0)
    insert into foo(value,absolute) values (7,1)
    insert into foo(value,absolute) values (3,0)
    go
    select sum(value) from foo where absolute  =0

    HTH

    Regards Alexander

    Saturday, November 7, 2020 6:22 PM