简体   繁体   中英

using self-join to pivot table in MySql

I need to pivot a table like this:

mediaID q_short_name start_time stop_time audio
 ee     A            208      210           j.mp3
 ee     B            308      310           j.mp3
 ff     A            124      127           k.mp3
 ff     B            166      169           k.mp3
 gg     C            582      584           p.mp3
 gg     B            489      492           p.mp3
 hh     D            222      225           q.mp3
 hh     C            121      124           q.mp3

to this:

mediaID Astart Astop Bstart Bstop Cstart Cstop Dstart Dstop audio
ee      208    210  308     310                              k.mp3
ff      124    127  166     169                              j.mp3
gg                   489    492    582   584                 p.mp3
hh                                 121   124    222    225   q.mp3

The following code from @Rick James in an earlier question how to pivot a table in mysql using model works but I need to expand it:

SELECT  c.mediaID,
    c.start_time AS CVVstart,
    c.end_time   AS CVVstop,
    e.start_time AS ExpStart,
    e.stop_time AS ExpStop,
    c.audio_file
FROM  my_test AS c
JOIN  my_test AS e  USING(mediaID)
WHERE  c.q_short_name = 'A'
  AND  e.q_short_name = 'B';

Per the solution below I tried this (I'm now adding the actual column names rather than the abbreviated letter in my example):

 SELECT DISTINCT 
 O.mediaID, 
 O.start_time AS CVVStart,
 O.stop_time AS CVVStop, 
 O.start_time AS ExpStart, 
 O.stop_time AS ExpStop,
 O.start_time AS CredCardCVVStart, 
 O.stop_time AS CredCardCVVStop,
 O.start_time AS CredCardNumStart,
 O.stop_time AS CredCArdNumStop, O.audio_link FROM my_test
 AS O
 LEFT JOIN my_test AS CVVStart
 ON CVVStart.mediaID = O.mediaID
  AND CVVStart.q_short_name = 'CVV Number - Start and Stop Time'

LEFT JOIN my_test AS CVVStop
ON CVVStop.mediaID = O.mediaID
AND CVVStart.q_short_name = 'CVV Number - Start and Stop Time'

LEFT JOIN my_test AS ExpStart
ON ExpStart.mediaID = O.mediaID
AND ExpStart.q_short_name = 'Expiration Date - Start and Stop Time'

LEFT JOIN my_test AS ExpStop
ON ExpStop.mediaID = O.mediaID
AND ExpStop.q_short_name = 'Expiration Date - Start and Stop Time'

LEFT JOIN my_test AS CredCardCVVStart
ON CredCardCVVStart.mediaID = O.mediaID
AND CredCardCVVStart.q_short_name = 'Credit Card CVV - Start and Stop 
Time'

LEFT JOIN my_test AS CredCardCVVStop
ON CredCardCVVStop.mediaID = O.mediaID
AND CredCardCVVStop.q_short_name = 'Credit Card CVV - Start and Stop 
Time'

LEFT JOIN my_test AS CredCardNumStart
ON CredCardNumStart.mediaID = O.mediaID
AND CredCardNumStart.q_short_name = 'Credit Card Number - Start and Stop 
Time'; 

I must be doing something wrong because I'm not getting distinct media IDs and also every cell is populated, whereas some of them should be empty. Eg in the first row I'm getting 308 and 310 repeated for each, which should only be the values for one, in this case 'B' or Expiration Date. If anyone could point me to what I'm doing wrong in the logic here, I'd appreciate it.

You can get this result by following query:

SELECT
  O.mediaID,
  -- start and stop for needed short_name here
  O.audio
FROM (
  SELECT DISTINCT mediaID, audio FROM my_test
) AS O
  LEFT JOIN my_test AS A
  ON A.mediaID = O.mediaID
  AND A.q_short_name = 'A'

  LEFT JOIN my_test AS B
  ON B.mediaID = O.mediaID
  AND B.q_short_name = 'B'

  LEFT JOIN my_test AS C
  ON C.mediaID = O.mediaID
  AND C.q_short_name = 'C'

Here we get distinct media ids and then join this table for any q_short_name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM