1

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!

4

1 回答 1

1

根据您当前的查询,如果每个 id_article 上有多个部分,我对子查询进行了一些更改以识别最大值。此外,0 应包含在 SectionA 的列表中

SELECT e1.id_article, 
       MAX(CASE WHEN section = 'SectionA' 
                THEN CONCAT(e2.id_section, ' (', e2.date, ')') END) AS sectionA, 
       MAX(CASE WHEN section = 'SectionB' 
                THEN CONCAT(e2.id_section, ' (', e2.date, ')') END) AS sectionB, 
       MAX(CASE WHEN section = 'SectionC'
                THEN CONCAT(e2.id_section, ' (', e2.date, ')') END) AS sectionC
  FROM event e1
 INNER JOIN (SELECT a.id_article,
                    a.id_section,       
                    a.`date`,
                    a.section,
                    COUNT(*) row_number
               FROM (SELECT *,
                            CASE WHEN id_section IN (0,1,2,3,4,5)
                                 THEN 'SectionA'
                                 WHEN id_section IN (6,7,8,9,10)
                                 THEN 'SectionB'
                                 WHEN id_section IN (11,12,13,14,15)
                                 THEN 'SectionC'
                             END section
                       FROM `event`) a
              INNER JOIN  (SELECT *,
                                  CASE WHEN id_section IN (0,1,2,3,4,5)
                                       THEN 'SectionA'
                                       WHEN id_section IN (6,7,8,9,10)
                                       THEN 'SectionB'
                                       WHEN id_section IN (11,12,13,14,15)
                                       THEN 'SectionC'
                                   END section
                             FROM `event`) b
                 ON a.id_article = b.id_article
                AND a.section = b.section
                AND a.`date` <= b.`date`
              GROUP BY a.id_article,
                       a.id_section,       
                       a.`date`
            ) e2
    ON e1.id_article = e2.id_article 
   AND e1.date = e2.date
 WHERE e2.row_number = 1
 GROUP BY e1.id_article

结果

id_article  sectionA    sectionB    sectionC
1           3 (2012)    (null)      (null)
2           (null)      6 (2014)    (null)
3           0 (2017)    (null)      13 (2016)
于 2017-09-16T23:18:54.260 回答