Skip to content
Advertisement

SQL – joining 3 tables and choosing newest logged entry per id

I got rather complicated riddle to solve. So far I’m unlocky.

I got 3 tables which I need to join to get the result.

Most important is that I need highest h_id per p_id. h_id is uniqe entry in log history. And I need newest one for given point (p_id -> num).

Apart from that I need ext and name as well.

history

+----------------+---------+--------+
|     h_id       |  p_id   | str_id |
+----------------+---------+--------+
|     1          | 1       |  11    |
|     2          | 5       |  15    |
|     3          | 5       |  23    |
|     4          | 1       |  62    |
+----------------+---------+--------+

point

+----------------+---------+
|     p_id       |  num    | 
+----------------+---------+
|     1          | 4564    | 
|     5          | 3453    |
+----------------+---------+

street

+----------------+---------+-------------+
|     str_id     |  ext    |     name    |
+----------------+---------+-------------+
|     15         |         | Mein st. 33 | - bad name
|     11         |         | eck  st. 42 | - bad name
|     62         | abc     | Main st. 33 |
|     23         | efg     | Back st. 42 |
+----------------+---------+-------------+

EXPECTED RESULT

+----------------+---------+-------------+-----+
|     num        |  ext    |     name    |h_id |
+----------------+---------+-------------+-----+
|     3453       | efg     | Back st. 42 | 3   |
|     4564       | abc     | Main st. 33 | 4   |
+----------------+---------+-------------+-----+

I’m using Oracle SQL. Tried using query below but result is not true.

Advertisement

Answer

In Oracle, you can use keep:

The keep syntax allows you to do “first()” and “last()” for aggregations.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement