Это можно сделать, используя текущую СУММ. Не хватает одной информации. Вы предполагаете, что данные хранятся в указанном вами порядке, но это не фиксируется ни в одном столбце. Итак, для моего примера я добавил столбец transaction_id. Теперь я могу гарантировать такой же порядок в избранном. Если столбец даты будет содержать временную часть (что, вероятно, есть, но это не включено в выборку данных), тогда ORDER BY dt)
будет достаточно.
create table so_students
(student_id NUMBER,
dt DATE,
book_id NUMBER,
checkout VARCHAR2(1),
total_books NUMBER,
active_books NUMBER,
recurring_update NUMBER,
update_status VARCHAR2(100),
transaction_id NUMBER
);
create sequence so_students_s;
INSERT INTO so_students values (101,TO_DATE('14-Apr-20','DD-Mon-YY'), 2,'N', 4, 1,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('17-Apr-20','DD-Mon-YY'), 2,'Y', 4, 0,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('17-Apr-20','DD-Mon-YY'), 4,'N', 4, 1,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('24-Apr-20','DD-Mon-YY'), 1,'N', 4, 2,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('24-Apr-20','DD-Mon-YY'), 3,'N', 4, 3,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('24-Apr-20','DD-Mon-YY'), 4,'Y', 4, 2,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('27-Apr-20','DD-Mon-YY'), 1,'Y', 4, 1,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('27-Apr-20','DD-Mon-YY'), 3,'Y', 4, 0,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('27-Apr-20','DD-Mon-YY'), 4,'N', 4, 1,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('1-May-20','DD-Mon-YY'), 1,'N', 4, 2,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('1-May-20','DD-Mon-YY'), 2,'N', 4, 3,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('1-May-20','DD-Mon-YY'), 4,'Y', 4, 2,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('3-May-20','DD-Mon-YY'), 3,'N', 4, 3,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('3-May-20','DD-Mon-YY'), 4,'N', 4, 4,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('4-May-20','DD-Mon-YY'), 2,'Y', 4, 3,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('4-May-20','DD-Mon-YY'), 4,'Y', 4, 2,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('8-May-20','DD-Mon-YY'), 1,'Y', 4, 1,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('10-May-20','DD-Mon-YY'), 2,'N', 4, 2,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('10-May-20','DD-Mon-YY'), 3,'Y', 4, 1,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('17-May-20','DD-Mon-YY'), 1,'N', 4, 2,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('18-May-20','DD-Mon-YY'), 1,'Y', 4, 1,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('18-May-20','DD-Mon-YY'), 2,'Y', 4, 0,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('18-May-20','DD-Mon-YY'), 4,'N', 4, 1,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('19-May-20','DD-Mon-YY'), 3,'N', 4, 2,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('19-May-20','DD-Mon-YY'), 4,'Y', 4, 1,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('22-May-20','DD-Mon-YY'), 3,'Y', 4, 0,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('22-May-20','DD-Mon-YY'), 4,'N', 4, 1,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('27-May-20','DD-Mon-YY'), 3,'N', 4, 2,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('27-May-20','DD-Mon-YY'), 4,'Y', 4, 1,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('28-May-20','DD-Mon-YY'), 2,'N', 4, 2,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('28-May-20','DD-Mon-YY'), 3,'Y', 4, 1,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('7-Jun-20','DD-Mon-YY'), 1,'N', 4, 2,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('7-Jun-20','DD-Mon-YY'), 2,'N', 4, 3,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('9-Jun-20','DD-Mon-YY'), 4,'N', 4, 4,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('14-Jun-20','DD-Mon-YY'), 1,'N', 4, 4,5,'INVALID UPDATE',so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('14-Jun-20','DD-Mon-YY'), 4,'N', 4, 4,6,'INVALID UPDATE',so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('15-Jun-20','DD-Mon-YY'), 1,'Y', 4, 3,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('15-Jun-20','DD-Mon-YY'), 4,'N', 4, 4,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('28-Jun-20','DD-Mon-YY'), 3,'N', 4, 4,5,'INVALID UPDATE',so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('28-Jun-20','DD-Mon-YY'), 4,'Y', 4, 3,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('29-Jun-20','DD-Mon-YY'), 2,'N', 4, 4,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('29-Jun-20','DD-Mon-YY'), 3,'Y', 4, 3,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('2-Jul-20','DD-Mon-YY'), 1,'N', 4, 4,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('2-Jul-20','DD-Mon-YY'), 2,'N', 4, 4,5,'INVALID UPDATE',so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('6-Jul-20','DD-Mon-YY'), 1,'Y', 4, 3,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('6-Jul-20','DD-Mon-YY'), 2,'N', 4, 4,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('23-Jul-20','DD-Mon-YY'), 2,'N', 4, 4,5,'INVALID UPDATE',so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('24-Jul-20','DD-Mon-YY'), 1,'N', 4, 4,6,'INVALID UPDATE',so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('24-Jul-20','DD-Mon-YY'), 2,'N', 4, 4,7,'INVALID UPDATE',so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('28-Jul-20','DD-Mon-YY'), 1,'Y', 4, 3,NULL,NULL,so_students_s.NEXTVAL);
INSERT INTO so_students values (101,TO_DATE('28-Jul-20','DD-Mon-YY'), 2,'N', 4, 2,NULL,NULL,so_students_s.NEXTVAL);
SELECT SUM(
CASE update_status
WHEN 'INVALID UPDATE' THEN
0
ELSE
CASE checkout
WHEN 'N' THEN
1
ELSE
- 1
END
END
) OVER(
ORDER BY dt,transaction_id) AS running_sum,
active_books,
s.*
FROM so_students s;