Group SQL queries

Asked

Viewed 93 times

3

I have these queries below, independent, and I’m having difficulty grouping them a same query result, IE, leave the 6 columns requested by select in the same consultation.

SELECT codigo_processo
      ,nome_produto_processo
      ,aprovado_uso
  FROM pqin
 WHERE codigo_processo = 22;

SELECT caminho_arquivo AS boletim tecnico
  FROM pqaq
 WHERE tipo_arquivo = 'BT'
   AND codigo_processo = 22;

SELECT caminho_arquivo AS fispq
  FROM pqaq
 WHERE tipo_arquivo = 'MS'
   AND codigo_processo = 22;

SELECT caminho_arquivo AS ficha de compensacao
  FROM pqaq
 WHERE tipo_arquivo = 'FC'
   AND codigo_processo = 22;

Observing: codigo_processo of pqin corresponds with codigo_processo of pqaq

  • Was the answer helpful to you? Don’t forget to mark it so it can be used if someone has a similar question!

1 answer

3

Basically you just have to do LEFT JOIN with herself:

SELECT base.codigo_processo,
       base.nome_produto_processo,
       base.aprovado_uso,
       boletim.caminho_arquivo AS 'Boletim Tecnico',
       fispq.caminho_arquivo AS fispq
       ficha.caminho_arquivo AS 'Ficha de Compensacao'
FROM pqin base
     LEFT JOIN pqaq boletim ON boletim.codigo_processo = base.codigo_processo
     LEFT JOIN pqaq fispq ON fispq.codigo_processo = base.codigo_processo
     LEFT JOIN pqaq ficha ON ficha.codigo_processo = base.codigo_processo
WHERE pqin.codigo_processo = 22
  AND boletim.tipo_arquivo = 'BT'
  AND fispq.tipo_arquivo = 'MS'
  AND ficha.tipo_arquivo = 'FC'

Browser other questions tagged

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