I have this table in my sql:
1 | user1 | 2023-07-10 |
2 | user1 | 2023-06-25 |
3 | user2 | 2023-06-21 |
4 | user3 | 2023-06-27 |
5 | user3 | 2023-07-11 |
6 | user4 | 2023-07-14 |
7 | user2 | 2023-07-16 |
8 | user1 | 2023-07-17 |
9 | user4 | 2023-07-15 |
10 | user5 | 2023-03-06 |
I need to calculate the difference for every consecutive date but per user (something like this):
user1 | 2023-06-25 | 2023-07-10 | 15 |
user1 | 2023-06-10 | 2023-07-17 | 7 |
user2 | 2023-06-21 | 2023-07-16 | 25 |
user3 | 2023-06-27 | 2023-07-11 | 14 |
and so on. I already searched online but found only solutions without per user.
UPDATE:
I arrived at this query that is very good but the output have multiple times the same date as start which I don’t want:
SELECT
p1.id AS id,
p1.name AS user,
p1.date AS date1,
p2.date AS date2,
DATEDIFF(p2.date, p1.date) AS date_difference
FROM prova p1
JOIN prova p2 ON p1.name = p2.name AND p2.date > p1.date
ORDER BY p1.name, p1.date;
2
Answers
Found a solution:
SQL Fiddle
use a querie like this
sample
http://sqlfiddle.com/#!9/aa0faf69/34