Group views in SQL Oracle

Asked

Viewed 93 times

1

I’m having a little logical reasoning block. I have a table (Tabela1) organized this way, but I would need to return the query as the table2 below. Someone can help me?

TABELA 1

inserir a descrição da imagem aqui

  • Search by PIVOT http://www.oracle.com/technetwork/pt/articles/sql/principais-caracteristicas-database-21083-ptb.html and CASE http://stackoverflow.com/questions/4841718/oracle-sql-pivot-query

2 answers

0

What you seek is to turn rows into columns, which can be reached through the PIVOT

I made an example for you to achieve your goal:

create table teste (
  codigo number, 
  nome varchar2(50),
  uso varchar2(10),  
  arquivo varchar2(50), 
  tipo varchar2(50)
);
insert into teste values (10, 'Joao', 'SIM', 'C:JOAO/REALT', 'REALT');

insert into teste values (10, 'Joao', 'SIM', 'C:JOAO/BOLET', 'BOLET');

insert into teste values (10, 'Joao', 'SIM', 'C:JOAO/CHAM', 'CHAM');

Final query:

select * from teste
pivot (
       max(arquivo)
       for tipo in ('REALT'  as REAT,
                    'CHAM'   as CHAM,
                    'BOLET'  as BOLET
      ));

Pivot Oracle

-2

SELECT PQ.CODIGO, PQ.NOME, PQ.USO, 
DECODE(PA.TIPO, 'REALT', PA.ARQUIVO) AS REALT, 
DECODE(PA.TIPO, 'BOLET', PA.ARQUIVO) AS BOLET, 
DECODE(PA.TIPO, 'CHAM', PA.ARQUIVO) AS CHAM FROM PQ, PA;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.