By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE test (
name varchar(20),
week_no int,
year_no int
);
INSERT INTO test (name, week_no, year_no)
VALUES ('fb', 5, 2021),
('twitter', 1, 2023),
('twitter', 2, 2022),
('twitter', 3, 2022),
('twitter', 7, 2022),
('youtube', 21, 2022);
Records: 6 Duplicates: 0 Warnings: 0
select * from test;
name | week_no | year_no |
---|---|---|
fb | 5 | 2021 |
1 | 2023 | |
2 | 2022 | |
3 | 2022 | |
7 | 2022 | |
youtube | 21 | 2022 |
SELECT t1.name,t1.year_no
FROM test t1
INNER JOIN test t2 ON t1.name=t2.name AND t1.year_no=t2.year_no
INNER JOIN test t3 ON t1.name=t3.name AND t1.year_no=t3.year_no
WHERE t2.week_no = t1.week_no + 1
AND t3.week_no = t1.week_no + 2
name | year_no |
---|