Hi @Mayur
You can create a temptable ,then use CURSOR and store each row into the temptable,please check :
DROP TABLE RESULTTABLE
CREATE TABLE RESULTTABLE
(Datestr varchar(20),
Rejection int,
Days int,
RUNNINGTOTAL int
)
DECLARE @DATESTR VARCHAR(20)
DECLARE @REJECTION INT
DECLARE @DAYS INT
DECLARE @SUM INT =0
DECLARE db_cursor CURSOR FOR
SELECT Datestr,Rejection, Days
FROM TEMPTABLE
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @DATESTR,@REJECTION, @DAYS
WHILE @@FETCH_STATUS = 0
BEGIN
IF @REJECTION = 0
BEGIN
SET @SUM = @SUM+@DAYS
END
ELSE
BEGIN
SET @SUM =@DAYS/(@REJECTION+1)
END
INSERT INTO RESULTTABLE
SELECT @DATESTR,@REJECTION,@DAYS,@SUM
FETCH NEXT FROM db_cursor INTO @DATESTR,@REJECTION, @DAYS
END
CLOSE db_cursor
DEALLOCATE db_cursor
SELECT * FROM RESULTTABLE
Result:
Best Regards,
Isabella
If the answer is the right solution, please click "Accept Answer" and upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.