I have a table as shown below. I want to do a partition and then subtract the values in the same column to get the difference.
Since there is no partition or equivalent function available in MySQL, can anyone give me an idea of how to do it? I have worked out the partition but not the other part.
SELECT ID,Date,
@row_number:=CASE WHEN @ID=ID THEN @row_number+1 ELSE 1 END AS row_number,
@ID:=ID AS ID,value
FROM table1, (SELECT @row_number:=0,@ID:='') AS t
ORDER BY id,Date;
Input:
ID Date Value
1001 24-07-2017 09:43 10
1002 24-07-2017 09:43 11
1003 22-08-2017 21:42 10
1001 07-09-2017 20:33 11
1003 07-09-2017 20:33 44
1002 24-07-2017 09:43 55
1004 07-09-2017 20:33 66
Output should be:
rowno ID Date Value Diff
1 1001 24-07-2017 09:43 10 N/A
2 1001 07-09-2017 20:33 11 1
1 1002 24-07-2017 09:43 11 N/A
2 1002 24-07-2017 09:43 55 44
1 1003 22-08-2017 21:42 10 n/A
2 1003 07-09-2017 20:33 44 34
1 1004 07-09-2017 20:33 66 N/A