Select über zwei Tabellen mit MAX()

Guten Tag,

ich habe ein Problem mit einem Select und bin hier auch durch eine Suche im Internet nicht weitergekommen.

Ich habe zwei Tabellen

article und ticket

ticket

id | tn | title
1 | 2018010101000123 | Geht nicht
2 | 2018010101000124 | Tut nicht
3 | 2018010101000125 | Weiss nicht

article

id | ticket_id | body
1 | 1 | Text 1
2 | 2 | Text 1
3 | 3 | Text 1
4 | 1 | Text 2
5 | 2 | Text 2
7 | 3 | Text 2
8 | 1 | Text 3
9 | 2 | Text 3
10 | 3 | Text 3
11 | 1 | Text 4
12 | 2 | Text 4
15 | 3 | Text 4
72 | 1 | Text 5
78 | 2 | Text 5
80 | 3 | Text 5
85 | 1 | Text 6

Ich brauche nun einen Select über beide Tabellen der mir tn und title aus der Tabelle ticket liefert und dazu die 4 letzten Einträge von body aus der Tabelle article zu jedem ticket.

Also:
id | tn | title | body
1 | 2018010101000123 | Geht nicht | Text 3
1 | 2018010101000123 | Geht nicht | Text 4
1 | 2018010101000123 | Geht nicht | Text 5
1 | 2018010101000123 | Geht nicht | Text 6
2 | 2018010101000124 | Tut nicht | Text 2
2 | 2018010101000124 | Tut nicht | Text 3
2 | 2018010101000125 | Tut nicht | Text 4
2 | 2018010101000124 | Tut nicht | Text 5
3 | 2018010101000125 | Tut nicht | Text 2
3 | 2018010101000125 | Tut nicht | Text 3
3 | 2018010101000125 | Tut nicht | Text 4
3 | 2018010101000125 | Tut nicht | Text 5

Hab es schon mit MAX() und Limit versucht, komme aber nicht wirklich weiter.
Die Tabellen zu joinen hat nicht geklappt, mit Limit komme ich auch nicht weiter und mit Suquerys kenn ich mich nicht gut genug aus…

Vielen Dank für Tips hierzu.

LG

Peter

Datensätze in einer relationalen Datenbank habe keine “Sortierung”.

" die 4 letzten Einträge" gibt es also nicht solange man keine Sortierung festlegt - anhand Deiner Beispieldaten gehe ich mal davon aus, dass sich das auf die ID Spalte in der Tabelle “article” bezieht.

Das kann man recht einfach mit einer Window Function lösen:

select t.id, t.tn, a.title, a.body
from ticket t
  join (
    select *, row_number() over (partition by ticket_id order by id desc) as rn
    from article
  ) a on a.ticket_id = t.id and a.rn <= 4
order by t.id, a.id;

Online Beispiel: http://rextester.com/BTR58187

test=*# select * from ticket ;
 id |         tn         |    title     
----+--------------------+--------------
  1 |  2018010101000123  |  Geht nicht
  2 |  2018010101000124  |  Tut nicht
  3 |  2018010101000125  |  Weiss nicht
(3 Zeilen)

test=*# select * from article;
 id | ticket_id |  body   
----+-----------+---------
  1 |         1 |  Text 1
  2 |         2 |  Text 1
  3 |         3 |  Text 1
  4 |         1 |  Text 2
  5 |         2 |  Text 2
  7 |         3 |  Text 2
  8 |         1 |  Text 3
  9 |         2 |  Text 3
 10 |         3 |  Text 3
 11 |         1 |  Text 4
 12 |         2 |  Text 4
 15 |         3 |  Text 4
 72 |         1 |  Text 5
 78 |         2 |  Text 5
 80 |         3 |  Text 5
 85 |         1 |  Text 6
(16 Zeilen)

test=*# select id, tn, title, body from (select t.id, t.tn, t.title, a.body, row_number() over (partition by t.id order by a.id desc) from article a left join ticket t on t.id=a.ticket_id order by id) foo where row_number <= 4 order by id, row_number desc;
 id |         tn         |    title     |  body   
----+--------------------+--------------+---------
  1 |  2018010101000123  |  Geht nicht  |  Text 3
  1 |  2018010101000123  |  Geht nicht  |  Text 4
  1 |  2018010101000123  |  Geht nicht  |  Text 5
  1 |  2018010101000123  |  Geht nicht  |  Text 6
  2 |  2018010101000124  |  Tut nicht   |  Text 2
  2 |  2018010101000124  |  Tut nicht   |  Text 3
  2 |  2018010101000124  |  Tut nicht   |  Text 4
  2 |  2018010101000124  |  Tut nicht   |  Text 5
  3 |  2018010101000125  |  Weiss nicht |  Text 2
  3 |  2018010101000125  |  Weiss nicht |  Text 3
  3 |  2018010101000125  |  Weiss nicht |  Text 4
  3 |  2018010101000125  |  Weiss nicht |  Text 5
(12 Zeilen)

test=*#

Einen Kaffee bitte!

Guten Tag,

vielen vielen Dank.
Passt perfekt!

LG

Peter