Mysql
 SQL >> Database >  >> RDS >> Mysql

Selezionare colonne sopra soglia in tabella non normalizzata

Se gli studenti sono memorizzati come colonne, la tabella non è normalizzata. Puoi comunque fare un pivot tramite una tabella derivata:

SELECT sub.student
FROM (
  SELECT t.timestamp, c.col AS student,
         CASE c.col
           WHEN 'Student1' THEN Student1
           WHEN 'Student2' THEN Student2
           WHEN 'Student3' THEN Student3
         END AS d
  FROM mytable t
  CROSS JOIN (SELECT 'Student1' col UNION ALL SELECT 'Student2' UNION ALL SELECT 'Student3') c
) sub
WHERE sub.timestamp = '20150911' AND sub.d > 0;

La soluzione migliore resta normalizzare i dati in righe con colonna studente e valore.