Union of two selects presenting online

Asked

Viewed 53 times

0

Below is a query performed for ORACLE database:

SELECT
ITOS.CD_NUMERO_OS,
ITOS.DESCRICAO_RECLA AS "DEFEITO APRESENTADO"

FROM GMITEMOS ITOS

INNER JOIN GMOSERVI OS ON OS.CD_NUMERO_OS = ITOS.CD_NUMERO_OS
INNER JOIN GMITEMOS IT ON IT.CD_NUMERO_OS = ITOS.CD_NUMERO_OS

WHERE OS.DT_OS BETWEEN TO_DATE(:data_os_inicial, 'DD/MM/YYYY') AND TO_DATE(:data_os_final, 'DD/MM/YYYY')
AND OS.SITUACAO LIKE 'E'
AND IT.TIPO_OS LIKE '3'

UNION

SELECT DISTINCT
ITOS.CD_NUMERO_OS,
SL.DESCRICAO_SOLUC AS "SOLUCAO DO DEFEITO"

FROM GMITEMOS ITOS

INNER JOIN GMOSERVI OS ON OS.CD_NUMERO_OS = ITOS.CD_NUMERO_OS
INNER JOIN GMITEMOS IT ON IT.CD_NUMERO_OS = ITOS.CD_NUMERO_OS
INNER JOIN GMDETALH SL ON SL.CD_NUMERO_OS = ITOS.CD_NUMERO_OS

WHERE OS.DT_OS BETWEEN TO_DATE(:data_os_inicial, 'DD/MM/YYYY') AND TO_DATE(:data_os_final, 'DD/MM/YYYY')
AND OS.SITUACAO LIKE 'E'
AND IT.TIPO_OS LIKE '3'

Below is a PRINT of how the information is being delivered on screen: Resultado da consulta citada acima

As we can observe, a DEFECT is presented and soon after a SOLUTION to the same number of OS (CD_NUMERO_OS). For example in the OS with CD_NUMERO_OS = 860, item 1 of the grid is a DEFECT and item 2 is a SOLUTION. I need to display DEFECT AND SOLUTION side by side, display in column format and not in rows according to the above print.

Any idea how I can display this result side by side???

Detail: A OS with CD_NUMERO_OS = 866 appears 3 times a DEFECT and 3 times a SOLUTION because the same OS has 3 different items.

2 answers

1


Rewrite your select this way:

SELECT
    
ITOS.CD_NUMERO_OS,
ITOS.DESCRICAO_RECLA AS "DEFEITO APRESENTADO",
NVL(SL.DESCRICAO_SOLUC, 'NAO RESOLVIDO') AS "SOLUCAO DO DEFEITO"

FROM GMITEMOS ITOS,    
GMOSERVI OS,
GMITEMOS IT, 
GMDETALH SL

WHERE OS.DT_OS BETWEEN TO_DATE(:data_os_inicial, 'DD/MM/YYYY') AND TO_DATE(:data_os_final, 'DD/MM/YYYY')
AND OS.CD_NUMERO_OS = ITOS.CD_NUMERO_OS
AND IT.CD_NUMERO_OS = ITOS.CD_NUMERO_OS
AND IT.ITEM = ITOS.ITEM /* faltou esse */
AND SL.CD_NUMERO_OS(+) = ITOS.CD_NUMERO_OS
AND OS.SITUACAO LIKE 'E'
AND IT.TIPO_OS LIKE '3'

I prefer to use equijoins, because I’ve seen cases (mainly in SQL mounted by the Entityframework of the .NET platform) that with Inner Join is slower, but then you can make a comparison and see which form is better.

See that to relate the table SL i used the (+) operator indicating a LEFT JOIN, because a defect may not have been solved

  • note: OS 866 will appear the same solution 3 times, because the solution is by OS and not by item

  • It worked great buddy, thank you very much! But look, I need to appear only 1 defect and 1 solution per OS item, as I mentioned earlier OS 866 has 3 items, so 3 defects and 3 solutions. Below is the result returned for the following query. Do you have any idea how else I can solve this "problem" ai?? rsrs @Jmslasher

  • guy just missed the item table relationship IT with the OS item table ITOS

  • question ITOS and IT are relating the same table, I believe that give only to reference the table ITOS and omit IT

0

SELECT DISTINCT
    
ITOS.CD_NUMERO_OS,
ITOS.ITEM AS "ITEM DA OS",
ITOS.DESCRICAO_RECLA AS "DEFEITO APRESENTADO",
NVL(SL.DESCRICAO_SOLUC, 'NAO RESOLVIDO') AS "SOLUCAO DO DEFEITO"

FROM GMITEMOS ITOS,    
GMOSERVI OS,
GMITEMOS IT, 
GMDETALH SL

WHERE OS.DT_OS BETWEEN TO_DATE(:data_os_inicial, 'DD/MM/YYYY') AND TO_DATE(:data_os_final, 'DD/MM/YYYY')
AND OS.CD_NUMERO_OS = ITOS.CD_NUMERO_OS
AND IT.CD_NUMERO_OS = ITOS.CD_NUMERO_OS
AND SL.CD_NUMERO_OS(+) = ITOS.CD_NUMERO_OS
AND OS.SITUACAO LIKE 'E'
AND IT.TIPO_OS LIKE '3'

ORDER BY itos.cd_numero_os ASC, itos.item ASC

Follow the print of the result. Resultado da consulta citada acima

  • Is this an answer or a supplement to the question? If it is not a solution of the problem then it is in the wrong place, should have edited the question and added the complement.

Browser other questions tagged

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