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.
CREATE TABLE my_table (
country VARCHAR(32),
value FLOAT,
date date
);
INSERT INTO
my_table
VALUES
('a', 1.4, '2010-02-03'),
('a', 1.4, '2010-02-04'),
('a', 1.3, '2010-02-05'),
('a', 1.4, '2010-02-06'),
('a', 1.2, '2010-02-07'),
('a', 1.4, '2010-02-08'),
('a', 1.5, '2010-02-09'),
('a', 1.7, '2010-02-10'),
('a', 1.4, '2010-02-11'),
('a', 1.6, '2010-02-12'),
('a', 1.4, '2010-02-13'),
('a', 1.5, '2010-02-14'),
('a', 1.3, '2010-02-15'),
('a', 1.2, '2010-02-16'),
('b', 1.3, '2010-02-03'),
('b', 1.3, '2010-02-04'),
('b', 1.4, '2010-02-05'),
('b', 1.6, '2010-02-06'),
('b', 1.9, '2010-02-07'),
('b', 1.3, '2010-02-08'),
('b', 1.3, '2010-02-09'),
('b', 1.2, '2010-02-10'),
('b', 1.3, '2010-02-11'),
('b', 1.5, '2010-02-12'),
('b', 1.3, '2010-02-13'),
('b', 1.3, '2010-02-14'),
('b', 1.5, '2010-02-15'),
('c', 1.4, '2010-02-03'),
('c', 1.4, '2010-02-04'),
('c', 1.3, '2010-02-05'),
('c', 1.4, '2010-02-06'),
('c', 1.2, '2010-02-07'),
('c', 1.4, '2010-02-08'),
Records: 69  Duplicates: 0  Warnings: 0
WITH
sorted AS
(
SELECT
*,
ROW_NUMBER() OVER (
PARTITION BY country
ORDER BY date -- choose a different column name!
) - 1
AS row_id,
COUNT(*) OVER (
PARTITION BY country
) - 1
AS step
FROM
my_table
)
SELECT
sorted.*
FROM
sorted
WHERE
mod( row_id * 4, step)
<
mod((row_id + step - 1) * 4, step)
ORDER BY
country, row_id
country value date row_id step
a 1.4 2010-02-03 0 13
a 1.2 2010-02-07 4 13
a 1.7 2010-02-10 7 13
a 1.4 2010-02-13 10 13
a 1.2 2010-02-16 13 13
b 1.3 2010-02-03 0 12
b 1.6 2010-02-06 3 12
b 1.3 2010-02-09 6 12
b 1.5 2010-02-12 9 12
b 1.5 2010-02-15 12 12
c 1.4 2010-02-03 0 11
c 1.4 2010-02-06 3 11
c 1.5 2010-02-09 6 11
c 1.6 2010-02-12 9 11
c 1.5 2010-02-14 11 11
d 1.3 2010-02-03 0 10
d 1.6 2010-02-06 3 10
d 1.3 2010-02-08 5 10
d 1.3 2010-02-11 8 10
d 1.3 2010-02-13 10 10
e 1.4 2010-02-03 0 9
e 1.4 2010-02-06 3 9
e 1.4 2010-02-08 5 9
e 1.7 2010-02-10 7 9
e 1.6 2010-02-12 9 9
f 1.3 2010-02-03 0 8
f 1.4 2010-02-05 2 8
f 1.9 2010-02-07 4 8
f 1.3 2010-02-09 6 8
f 1.3 2010-02-11 8 8