I have views which look something like this:
mysql> select * from p2;
+---+---------+---------+
| k | measure | time_id |
+---+---------+---------+
| D | 200 | 2 |
| E | 201 | 2 |
| F | 203 | 2 |
| A | 20 | 1 |
| B | 22 | 1 |
| C | 23 | 1 |
| D | 100 | 1 |
| E | 101 | 1 |
| F | 103 | 1 |
| G | 4 | 1 |
| H | 7 | 1 |
| I | 10 | 1 |
+---+---------+---------+
(k, time_id)
is a unique key, and the above is greatly simplified (there will be many more values of time_id
and k
). The sort order is time_id DESC
(followed by k ASC
, but that's not so important).
I want to find a SELECT statement that will filter it to this:
+---+---------+---------+
| k | measure | time_id |
+---+---------+---------+
| D | 200 | 2 |
| E | 201 | 2 |
| F | 203 | 2 |
| A | 20 | 1 |
| B | 22 | 1 |
| C | 23 | 1 |
| G | 4 | 1 |
| H | 7 | 1 |
| I | 10 | 1 |
+---+---------+---------+
I want to make sure that values for column k
are unique, by filtering out rows where the k
value has already been used before.
In this example, in the original view rows 0, 1, 2 contained k values D, E and F, but so did rows 6, 7, 8, so rows 6-8 are removed to make the second view.
Is there a SELECT statement that can do this? It feels like it should be straightforward, but I can't figure out how to do it.