Result of select feeding 2 other selects

0

I have select 1

SELECT DISTINCT
SD2.D2_DOC            AS NOTA,
CONCAT(RTRIM(SD2.D2_CLIENTE), ' - ', SA1.A1_NOME)        AS CLIENTE,
SD2.D2_COD              AS CODPRODUTO,
QEK_REVI                AS REVISÃO,
SD2.D2_LOTECTL          AS LOTE,
SB1.B1_DESC             AS DESCPRODUTO,
SB1.B1_X_ESPEC                                           AS NQUIMICO,
SC2.C2_XLOTEF         AS FORLOTE,
SC2.C2_XNPAIS         AS PAIS,
SC2.C2_XNOMFA         AS FABRICANTE,
CONVERT(VARCHAR(10), CAST(SC2.C2_XDTFAB AS DATE),103)    AS FABRICAÇÃO,
CONVERT(VARCHAR(20), CAST(SC2.C2_XDTVALI AS DATE),103)   AS VALIDADE,
SB1.B1_XDCB           AS DCB,
SB1.B1_XDCI           AS DCI,
SB1.B1_XCAS           AS CAS,
QE6.QE6_XMANUS        AS MANUSEIO,
QE6.QE6_XDERRA        AS DERRAMAMENTO,
QE6.QE6_XTRATA        AS TRATAMENTO,
QEK.QEK_CERQUA        AS NLAUDO
FROM SD2020 AS SD2
INNER JOIN SA1020 AS SA1 WITH(NOLOCK) ON SA1.A1_COD = SD2.D2_CLIENTE
INNER JOIN SB1020 AS SB1 WITH(NOLOCK) ON SB1.B1_COD = SD2.D2_COD
INNER JOIN SC2020 AS SC2 WITH(NOLOCK) ON SC2.C2_XLOTE = SD2.D2_LOTECTL
INNER JOIN QEK020 AS QEK WITH(NOLOCK) ON QEK.QEK_XORDEM = SC2.C2_NUM
INNER JOIN QE6020 AS QE6 WITH(NOLOCK) ON QE6.QE6_PRODUT = QEK.QEK_PRODUT
WHERE SD2.D2_DOC = '244788' AND SD2.D2_COD = '000878'
AND SA1.D_E_L_E_T_ = '' AND SB1.D_E_L_E_T_ = '' AND SD2.D_E_L_E_T_ = '' AND QEK.D_E_L_E_T_ = '' AND QE6.D_E_L_E_T_ = ''

Which brings me the following result.

CODPRODUTO  REVISÃO LOTE
000878          02  PS-010435/F01    

I need to use this first select I to feed the fields of 2 other selects and in the end they become a single result and possible?

2nd select that needs to be fed by 1st select.

SELECT
QE1.QE1_DESCPO    AS CARACTERISTICAS,
QE8.QE8_TEXTO     AS ESPECIFICAÇÕES,
QE8.QE8_METODO    AS METODO
FROM QE8020 AS QE8
INNER JOIN QE1010 AS QE1 WITH(NOLOCK) ON QE1.QE1_ENSAIO = QE8.QE8_ENSAIO 
WHERE QE8.QE8_PRODUT = '000878' AND QE8.QE8_REVI = '02' 
ORDER BY QE8_SEQLAB

3rd select that also needs to be fed by 1st select.

SELECT
QER_LOTE,
QER_REVI,
QEQ.QEQ_MEDICA   AS RESULTADO
FROM QER020 AS QER
INNER JOIN QEQ020 AS QEQ WITH(NOLOCK) ON QEQ.QEQ_CODMED = QER.QER_CHAVE
WHERE QER.QER_LOTE = 'PS-010435/F01'

Thank you in advance

    
asked by anonymous 14.06.2018 / 19:57

1 answer

0

Do this:

You must ensure that the link fields in the JOIN appear in the tables.

That the COD_PRODUCT and REVISAO fields are selected in SELECT A and SELECT B and that the QER_LOTE column is selected in both the A and C

SELECT * FROM  (

SELECT DISTINCT
SD2.D2_DOC            AS NOTA,
CONCAT(RTRIM(SD2.D2_CLIENTE), ' - ', SA1.A1_NOME)        AS CLIENTE,
SD2.D2_COD              AS CODPRODUTO,
QEK_REVI                AS REVISÃO,
SD2.D2_LOTECTL          AS LOTE,
SB1.B1_DESC             AS DESCPRODUTO,
SB1.B1_X_ESPEC                                           AS NQUIMICO,
SC2.C2_XLOTEF         AS FORLOTE,
SC2.C2_XNPAIS         AS PAIS,
SC2.C2_XNOMFA         AS FABRICANTE,
CONVERT(VARCHAR(10), CAST(SC2.C2_XDTFAB AS DATE),103)    AS FABRICAÇÃO,
CONVERT(VARCHAR(20), CAST(SC2.C2_XDTVALI AS DATE),103)   AS VALIDADE,
SB1.B1_XDCB           AS DCB,
SB1.B1_XDCI           AS DCI,
SB1.B1_XCAS           AS CAS,
QE6.QE6_XMANUS        AS MANUSEIO,
QE6.QE6_XDERRA        AS DERRAMAMENTO,
QE6.QE6_XTRATA        AS TRATAMENTO,
QEK.QEK_CERQUA        AS NLAUDO
FROM SD2020 AS SD2
INNER JOIN SA1020 AS SA1 WITH(NOLOCK) ON SA1.A1_COD = SD2.D2_CLIENTE
INNER JOIN SB1020 AS SB1 WITH(NOLOCK) ON SB1.B1_COD = SD2.D2_COD
INNER JOIN SC2020 AS SC2 WITH(NOLOCK) ON SC2.C2_XLOTE = SD2.D2_LOTECTL
INNER JOIN QEK020 AS QEK WITH(NOLOCK) ON QEK.QEK_XORDEM = SC2.C2_NUM
INNER JOIN QE6020 AS QE6 WITH(NOLOCK) ON QE6.QE6_PRODUT = QEK.QEK_PRODUT
WHERE SD2.D2_DOC = '244788' AND SD2.D2_COD = '000878'
AND SA1.D_E_L_E_T_ = '' AND SB1.D_E_L_E_T_ = '' AND SD2.D_E_L_E_T_ = '' AND QEK.D_E_L_E_T_ = '' AND QE6.D_E_L_E_T_ = '' ) A 

LEFT JOIN ( 

SELECT
QE1.QE1_DESCPO    AS CARACTERISTICAS,
QE8.QE8_TEXTO     AS ESPECIFICAÇÕES,
QE8.QE8_METODO    AS METODO
FROM QE8020 AS QE8
INNER JOIN QE1010 AS QE1 WITH(NOLOCK) ON QE1.QE1_ENSAIO = QE8.QE8_ENSAIO 
WHERE QE8.QE8_PRODUT = '000878' AND QE8.QE8_REVI = '02' 
ORDER BY QE8_SEQLAB ) B

ON A.COD_PRODUTO = B.COD_PRODUTO AND A.REVISAO = B.REVISAO 

LEFT JOIN ( 

SELECT
QER_LOTE,
QER_REVI,
QEQ.QEQ_MEDICA   AS RESULTADO
FROM QER020 AS QER
INNER JOIN QEQ020 AS QEQ WITH(NOLOCK) ON QEQ.QEQ_CODMED = QER.QER_CHAVE
WHERE QER.QER_LOTE = 'PS-010435/F01' ) C

ON A.QER_LOTE = C.A.QER_LOTE
    
14.06.2018 / 22:08