0

I have the below sql table

DECLARE @TESTTABLE TABLE(ID INT, CODE NVARCHAR(5), VALUE INT)
INSERT INTO @TESTTABLE
VALUES  (1,'C1',3), (2,'C1',4), (3,'C1',2), (4,'C2',4), (5,'C2',5), (6,'C2',7), (7,'C3',1), (8,'C4',3), (9,'C4',8)
SELECT * FROM @TESTTABLE

And I need the output would be - Sum over partition by with id and the value should be reset with code (this is not static and should be anything).

enter image description here

I have tried this below query but how to reset the value using code.

SELECT ID, CODE, VALUE, SUM(VALUE) OVER (ORDER BY ID) AS RESULT FROM @TESTTABLE
0

1 Answer 1

2

You were close. Just need to include a partition by

SELECT * 
      ,Result = sum(Value) over (partition by code order by id)
 FROM  @TESTTABLE

Results

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much John. Oh God, Cure my hangover.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.