I have the following SQL table called readings
.
date | today | yesterday | tomorrow | creationtime | source
2021-01-01 110 0.5 0 2021-01-01 12:42:17.... x1
2021-01-01 110 0.5 0 2021-01-01 12:42:17.... x2
2021-01-01 150 0.9 1 2021-01-01 12:55:17.... x3
....
2021-02-15 110 0.3 1 2021-02-15 12:42:17.... x1
2021-02-15 110 0.1 1 2021-02-15 12:42:17.... x2
2021-02-15 150 0.9 1 2021-02-15 12:55:17.... x3
...
2021-02-15 110 0.5 0 2021-02-16 16:06:04.008673 x17
2021-02-15 110 0.5 0 2021-02-15 15:59:46.383677 x17
....
2021-02-15 700 0.7 1 2021-02-16 16:04:02.267478 x20
2021-02-15 110 0.7 1 2021-02-15 15:59:48.060236 x20
....
2021-02-22 110 0.5 1 2021-02-15 16:01:16.826577 x55
2021-02-22 110 0.5 1 2021-02-16 16:09:17.524436 x55
There are 65 readings every day. Readings from x1, x2, x3... until x65.
So I found duplicate readings on certain days.
Sometimes the readings are different, so I want to keep the newer reading on that day, even though it was only recorded the following day.
I want to drop the duplicated values, I want to keep the newer creation time. So I want my table to end up looking like this.
date | today | yesterday | tomorrow | creationtime | source
2021-01-01 110 0.5 0 2021-01-01 12:42:17.... x1
2021-01-01 110 0.5 0 2021-01-01 12:42:17.... x2
2021-01-01 150 0.9 1 2021-01-01 12:55:17.... x3
....
2021-02-15 110 0.3 1 2021-02-15 12:42:17.... x1
2021-02-15 110 0.1 1 2021-02-15 12:42:17.... x2
2021-02-15 150 0.9 1 2021-02-15 12:55:17.... x3
...
2021-02-15 110 0.5 0 2021-02-16 16:06:04.008673 x17
....
2021-02-15 700 0.7 1 2021-02-16 16:04:02.267478 x20
....
2021-02-22 110 0.5 1 2021-02-16 16:09:17.524436 x55
I tried to do
create table new_readings as select distinct c.* from readings c;
But it just creates a copy of the table and drops values which are completely distinct.