1

I'm using Oracle and I want to turn a query that gives me several columns into several rows.

This my my query:

SELECT I.cd_curso, 
       I.cd_discip, 
       ND.ds_discip, 
       I.cd_turma_t, 
       I.cd_turma_p, 
       I.cd_turma_l, 
       I.cd_turma_tp, 
       I.cd_turma_e, 
       I.cd_turma_o, 
       I.cd_turma_c, 
       I.cd_turma_s, 
       Count(*) AS alunos 
FROM   cse.v_vwinscri I, 
       cse.t_tbdiscip ND 
WHERE  I.cd_lectivo = 201314 
       AND I.cd_discip = 911901 
       AND I.cd_discip = ND.cd_discip 
       AND I.cd_curso = 9885 
       AND ( I.cd_turma_t IS NOT NULL 
              OR I.cd_turma_p IS NOT NULL 
              OR I.cd_turma_l IS NOT NULL 
              OR I.cd_turma_tp IS NOT NULL 
              OR I.cd_turma_e IS NOT NULL 
              OR I.cd_turma_o IS NOT NULL 
              OR I.cd_turma_c IS NOT NULL 
              OR I.cd_turma_s IS NOT NULL ) 
GROUP  BY I.cd_curso, 
          I.cd_discip, 
          ND.ds_discip, 
          I.cd_turma_t, 
          I.cd_turma_p, 
          I.cd_turma_l, 
          I.cd_turma_tp, 
          I.cd_turma_e, 
          I.cd_turma_o, 
          I.cd_turma_c, 
          I.cd_turma_s 
ORDER  BY I.cd_curso, 
          I.cd_turma_t, 
          I.cd_turma_p, 
          I.cd_turma_l, 
          I.cd_turma_tp, 
          I.cd_turma_e, 
          I.cd_turma_o, 
          I.cd_turma_c, 
          I.cd_turma_s 

And I wish to turn the results CD_TURMA_T, CD_TURMA_P, and so on, into diferent rows as CD_TURMA. Well basically using the first fiels as keys and the others as values, changing them from columns to rows...

Is it possible?

2
  • Yes it is possible.Just look for tutorial on Oracle Pivot or stackoverflow.com/questions/17060044/… Commented Jan 30, 2014 at 12:59
  • It's shorter to write COALESCE(I.cd_turma_t, I.cd_turma_p, I.cd_turma_l, I.cd_turma_tp, ...) IS NOT NULL Commented Jan 30, 2014 at 13:01

1 Answer 1

1

You can simply use UNION ALL like this:

WITH cte AS (
    SELECT I.cd_curso, 
           I.cd_discip, 
           ND.ds_discip, 
           I.cd_turma_t, 
           I.cd_turma_p, 
           I.cd_turma_l, 
           I.cd_turma_tp, 
           I.cd_turma_e, 
           I.cd_turma_o, 
           I.cd_turma_c, 
           I.cd_turma_s, 
           Count(*) AS alunos 
    FROM   cse.v_vwinscri I, 
           cse.t_tbdiscip ND 
    WHERE  I.cd_lectivo = 201314 
           AND I.cd_discip = 911901 
           AND I.cd_discip = ND.cd_discip 
           AND I.cd_curso = 9885 
           AND ( I.cd_turma_t IS NOT NULL 
                  OR I.cd_turma_p IS NOT NULL 
                  OR I.cd_turma_l IS NOT NULL 
                  OR I.cd_turma_tp IS NOT NULL 
                  OR I.cd_turma_e IS NOT NULL 
                  OR I.cd_turma_o IS NOT NULL 
                  OR I.cd_turma_c IS NOT NULL 
                  OR I.cd_turma_s IS NOT NULL ) 
    GROUP  BY I.cd_curso, 
              I.cd_discip, 
              ND.ds_discip, 
              I.cd_turma_t, 
              I.cd_turma_p, 
              I.cd_turma_l, 
              I.cd_turma_tp, 
              I.cd_turma_e, 
              I.cd_turma_o, 
              I.cd_turma_c, 
              I.cd_turma_s)
SELECT cd_curso, 
       cd_discip, 
       ds_discip, 
       cd_turma_t AS cd_trauma,
       alunos
FROM   cte
UNION ALL 
SELECT cd_curso, 
       cd_discip, 
       ds_discip, 
       cd_turma_p,
       alunos
FROM   cte
UNION ALL
SELECT cd_curso, 
       cd_discip, 
       ds_discip, 
       cd_turma_l,
       alunos 
FROM   cte
UNION ALL
SELECT cd_curso, 
       cd_discip, 
       ND.ds_discip, 
       cd_turma_tp,
       alunos 
FROM   cte
UNION ALL
SELECT cd_curso, 
       cd_discip, 
       ds_discip, 
       cd_turma_e, 
       alunos 
FROM   cte
UNION ALL
SELECT cd_curso, 
       cd_discip, 
       ds_discip, 
       cd_turma_o, 
       alunos 
FROM   cte
UNION ALL
SELECT cd_curso, 
       cd_discip, 
       ds_discip, 
       cd_turma_c, 
       alunos 
FROM   cte
UNION ALL
SELECT cd_curso, 
       cd_discip, 
       ds_discip, 
       cd_turma_s,
       alunos 
FROM   cte;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.