I'm trying to create a simple user retention, I tried a lot of ways searching, but none of them seems efficient and most of them have a lot of calculations that I don't really need.
Input my table example: https://dbfiddle.uk/?rdbms=postgres_14&fiddle=ec68bebf63280023e828ebb6cabd2d89 OR:
DROP TABLE IF EXISTS users_churn;
create table
users_churn(
id SERIAL PRIMARY KEY,
users varchar(10) not null,
dates timestamp
);
insert into users_churn(users, dates)
values
('1ab7', '2022-01-04 10:22'),
('1ab7', '2022-04-26 18:30'),
('1ab7', '2022-05-12 20:10'),
('1ab7', '2022-07-02 20:55'),
('3ac5', '2022-02-05 05:12'),
('3ac5', '2022-04-09 07:17'),
('3ac5', '2022-07-03 04:19');
select * from users_churn
Output that I expect:
+-------------------------------------------------+
| --DESIRED OUTPUT |
+-------------------------------------------------+
| ('1ab7', '2022-01-04 10:22', 'first_purchase'), |
| ('1ab7', '2022-02-01 00:00', 'churn'), |
| ('1ab7', '2022-03-01 00:00', 'churn'), |
| ('1ab7', '2022-04-26 18:30', 'retained'), |
| ('1ab7', '2022-05-12 20:10', 'retained'), |
| ('1ab7', '2022-06-01 00:00', 'churn'), |
| ('1ab7', '2022-07-02 20:55', 'retained'), |
| ('3ac5', '2022-02-05 05:12', 'first_purchase'), |
| ('3ac5', '2022-03-01 00:00', 'churn'), |
| ('3ac5', '2022-04-09 07:17', 'retained'), |
| ('3ac5', '2022-05-01 00:00', 'churn'), |
| ('3ac5', '2022-06-01 00:00', 'churn'), |
| ('3ac5', '2022-07-03 04:19', 'retained'); |
+-------------------------------------------------+