I still can't figure this out even after hours of searching and trying. Maybe I did not choose the right words?
Okay, so I have table with events like this:
EVENT
+----+------------+------------+------+
| id | id_article | id_section | date |
+----+------------+------------+------+
| 1 | 1 | 1 | 2011 |
| 2 | 1 | 3 | 2012 |
| 3 | 2 | 7 | 2013 |
| 4 | 2 | 6 | 2014 |
| 5 | 3 | 14 | 2015 |
| 6 | 3 | 13 | 2016 |
| 7 | 3 | 0 | 2017 |
+----+------------+------------+------+
I would like to pivot this table into three different columns where each column contains latest events (id_section and date) within list of id_section. Well, basically like this:
sectionA = id_section IN (1,2,3,4,5) and MAX(date)
sectionB = id_section IN (6,7,8,9,10) and MAX(date)
sectionC = id_section IN (11,12,13,14,15) and MAX(date)
DESIRED RESULT
+------------+----------+----------+-----------+
| id_article | sectionA | sectionB | sectionC | #id_event
+------------+----------+----------+-----------+
| 1 | 3 (2012) | null | null | #id 2
| 2 | null | 6 (2014) | null | #id 4
| 3 | 0 (2017) | null | 13 (2016) | #id 7, 6 <-- two latest events
+------------+----------+----------+-----------+
Here is SqlFiddle of what I have right now - it only selects latest event per id_article:
INSUFFICIENT RESULT
+------------+----------+----------+-----------+
| id_article | sectionA | sectionB | sectionC | #id_event
+------------+----------+----------+-----------+
| 1 | 3 (2012) | null | null | #id 2
| 2 | null | 6 (2014) | null | #id 4
| 3 | 0 (2017) | null | null | #id 7 <-- only latest event
+------------+----------+----------+-----------+
I tried to use more JOINS but I still can't figure it out.
Thanks for any help!