Oracle
 sql >> Base de Dados >  >> RDS >> Oracle

colunas no oracle pivot


Para classificar os registros, você pode adicionar o seguinte ao final do seu sql
ORDER BY TO_DATE('01/'||month||'/'||year,'dd/mm/yyyy') 

Observação:os nomes das colunas dinâmicas diferenciam maiúsculas de minúsculas, portanto, você precisa citá-los

aqui está a consulta completa, selecionando apenas as colunas necessárias:
 SELECT mnth_txt,"Omsk_CNT","Omsk_SM","Novosibirsk_CNT",
        "Novosibirsk_SM","Orenburg_CNT","Orenburg_SM" 
 FROM
 (
 SELECT *
 FROM (     
  SELECT 1 cnt, 2 sm, '55' name, 12 month, 2011 year, '12 2011' mnth_txt FROM DUAL UNION ALL     
  SELECT 1 cnt, 2 sm, '54' name, 11 month, 2011 year, '11 2011' mnth_txt FROM DUAL UNION ALL    
  SELECT 1 cnt, 2 sm, '55' name, 11 month, 2011 year, '11 2011' mnth_txt FROM DUAL UNION ALL     
  SELECT 1 cnt, 2 sm, '54' name, 12 month, 2011 year, '12 2011' mnth_txt FROM DUAL UNION ALL     
  SELECT 1 cnt, 2 sm, '55' name, 12 month, 2011 year, '12 2011' mnth_txt FROM DUAL UNION ALL     
  SELECT 1 cnt, 2 sm, '56' name, 12 month, 2010 year, '12 2010' mnth_txt FROM DUAL   ) 
  PIVOT ( SUM(cnt) cnt, SUM(sm) sm     
          FOR NAME IN             
          ( '55' AS "Omsk",'54' AS "Novosibirsk", '56' AS "Orenburg" ) 
        )
  ORDER BY TO_DATE('01/'||month||'/'||year,'dd/mm/yyyy')         
 )