How can I delete duplicate data based on the common value (Start and End)
(Time is unique key)
My table is:
Time | Data |
---|---|
10:24:11 | Start |
10:24:12 | Result |
10:24:13 | Result |
10:24:14 | End |
10:24:15 | Start |
10:24:16 | Result |
10:24:17 | End |
I want to get Data: Result in between Start and End that is with the MAX(TIME) when duplication does occur. as such
The result that I want:
Time | Data |
---|---|
10:24:11 | Start |
10:24:13 | Result |
10:24:14 | End |
10:24:15 | Start |
10:24:16 | Result |
10:24:17 | End |
I have tried rearranging the data, but couldn't seems to get the result that I want, Could someone give their advice on this case?
Update
I ended up not using either of the the approach suggested by @fredt and @airliquide as my version of HSQLDB doesn't support the function.
so what I did was, adding sequence and making Start = 1, Result = 2, and End = 3.
Sequence | Time | Data | Indicator |
---|---|---|---|
1 | 10:24:11 | Start | 1 |
2 | 10:24:12 | Result | 2 |
3 | 10:24:13 | Result | 2 |
4 | 10:24:14 | End | 3 |
5 | 10:24:15 | Start | 1 |
6 | 10:24:16 | Result | 2 |
7 | 10:24:17 | End | 3 |
Thereon, I make use of the indicator and sequence to get only latest Result. Such that if previous row is 2 (which is result), remove it.
The guide that I follow: From: Is there a way to access the "previous row" value in a SELECT statement?
select t1.value - t2.value from table t1, table t2
where t1.primaryKey = t2.primaryKey - 1