add batch remove batch split batch comment selection show hidden batches hide batch highlight batch
db<>fiddle
donate feedback about
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
SELECT * INTO #data FROM (VALUES
(CAST('2021-08-15' AS DATE), 'Bulgaria', 1081636),
('2021-08-16', 'Bulgaria', 1084693),
('2021-08-17', 'Bulgaria', 1089066),
('2021-08-18', 'Bulgaria', NULL),
('2021-08-19', 'Bulgaria', NULL),
('2021-08-20', 'Bulgaria', 1189066)) x(dt, country, cnt)
6 rows affected
SELECT dt
, country
, cnt
, MAX(cnt) OVER (PARTITION BY country ORDER BY dt)
FROM #data
ORDER BY dt
dt country cnt (No column name)
2021-08-15 Bulgaria 1081636 1081636
2021-08-16 Bulgaria 1084693 1084693
2021-08-17 Bulgaria 1089066 1089066
2021-08-18 Bulgaria null 1089066
2021-08-19 Bulgaria null 1089066
2021-08-20 Bulgaria 1189066 1189066
Warning: Null value is eliminated by an aggregate or other SET operation.

SELECT dt
, country
, cnt
, SUM(cnt) OVER (PARTITION BY country, partition)
FROM (
SELECT country
, dt
, cnt
, SUM(CASE WHEN cnt IS NULL THEN 0 ELSE 1 END) OVER (PARTITION BY country ORDER BY dt) AS partition
FROM #data
) AS d
ORDER BY dt
dt country cnt (No column name)
2021-08-15 Bulgaria 1081636 1081636
2021-08-16 Bulgaria 1084693 1084693
2021-08-17 Bulgaria 1089066 1089066
2021-08-18 Bulgaria null 1089066
2021-08-19 Bulgaria null 1089066
2021-08-20 Bulgaria 1189066 1189066
Warning: Null value is eliminated by an aggregate or other SET operation.