I have a user table like this,
name week_no year_no
fb 5 2021
twitter 1 2022
twitter 2 2022
twitter 3 2022
twitter 7 2022
youtube 21 2022
I want to find the names of users who login >= 3 consecutive weeks in the same year. The week numbers will be unique for each year. For example, in the above table we can see that user twitter
is logged in week_no: 1, 2, 3
in the same year 2022
thereby satisfying the condition that I am looking for.
The output I am looking for,
name year_no
twitter 2022
You can create the sample table using,
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, 2022),
('twitter', 2, 2022),
('twitter', 3, 2022),
('twitter', 7, 2022),
('youtube', 21, 2022);
I am new to SQL language and I read that group by can achieve that, can someone help in what function/query we have to use to achieve the same.
select * from test group by year_no, name;
Thank you for any help in advance.
3
Answers
A simple solution which will work on every MySQL version, without using windows function. Join the same table 3 times
https://dbfiddle.uk/XjeXKUFE
Window function version.
demo
You may define a unique groups for consecutive weeks in the same year and aggregate them as the following:
See a demo.