SQL tabular conversion



  • Good afternoon! There is a table:

     eventidid |timestamp|

    event1-001-21-03-15
    event2-001-22-03-15
    event1-002-23-03-15
    event2-002-24-03-15

    What kind of request is required to result in:

    id  event1  event2

    001-21-03-15-22-03-15-
    002-23-03-15-24-03-15-

    I can't do this for the second week.
    Clearly one thing is, first have to make a sample of unique id:

    SELECT id FROM test GROUP BY id;

    And then like that:

    SELECT timestamp WHERE id IN (SELECT id FROM test GROUP BY id) AND event='event1';

    event known in advance (event1,event2)
    If there are repeated events under one id, different or equal timestamp, add columns to the result, for example:

    id  event1  event2  event1  event2

    001-21-03-15-22-03-15-23-03-15-23-03-15-23-03-15-
    002-23-03-15-24-03-15-NULL NULL



  • In general terms:

    SELECT A.id,GROUP_CONCAT(B.timestamp) AS event1,GROUP_CONCAT(C.timestamp) AS event2
      FROM (select distinct id from test) A
           LEFT JOIN test B ON B.id=A.id and B.event='event1'
           LEFT JOIN test C ON C.id=A.id and C.event='event2'
     GROUP BY A.id
    

    But in this form, the function will gather the time-stamps of recurring events for a specific id in the line, through the comma. If timestamp's not a line, then you're gonna have to turn to the line when it's on hand. group_concat♪ By default, the maximum length of the line of the formed group_concat - 1024 bayta.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2