Domanda

Ho un problema con il seguente SQL. Perché sulla terra meno di 2 esattamente le stesse domande restituirebbe un risultato non vuoto? Ho provato "Union All" invece dell'Unione, ho provato molte altre cose, nessuna delle quali ha funzionato. Si prega di avvisare.

    SELECT y.segment1 po_num, fad.seq_num seq, fdst.short_text st
              FROM applsys.fnd_attached_documents fad,
                   applsys.fnd_documents fd,
                   applsys.fnd_documents_short_text fdst,
                   po_headers_all y
             WHERE     1 = 1
                   AND fad.pk1_value(+) = y.po_header_id
                   AND fad.entity_name = 'PO_HEADERS'
                   AND fad.document_id = fd.document_id
                   AND fd.datatype_id = 1
                   and fad.seq_num>=100
                   AND fdst.media_id = fd.media_id
                   and y.type_lookup_code='STANDARD'
                   AND NVL(y.CANCEL_FLAG,'N')='N'
                 --  and y.segment1 in (100,1000,100,650,26268)
                --   and y.segment1=1000
            UNION 
            SELECT poh.segment1, 1, '1' --null, null
              FROM    po.po_headers_all poh
                   LEFT JOIN
                      (SELECT fad1.pk1_value
                         FROM applsys.fnd_attached_documents fad1,
                              applsys.fnd_documents fd1
                        WHERE     1 = 1
                              AND fad1.entity_name = 'PO_HEADERS'
                              AND fad1.document_id = fd1.document_id
                              and fad1.seq_num>=100
                              AND fd1.datatype_id = 1) sub1
                   ON poh.po_header_id = sub1.pk1_value
             WHERE sub1.pk1_value IS NULL 
                        and poh.type_lookup_code='STANDARD'
                        AND NVL(poh.CANCEL_FLAG,'N')='N'
                      --  and poh.segment1 in (100,1000,100,650,26268) 
                      --  and poh.segment1=1000                       
          --   and poh.segment1=650)              
          minus
   SELECT y.segment1 po_num, fad.seq_num seq, fdst.short_text st
              FROM applsys.fnd_attached_documents fad,
                   applsys.fnd_documents fd,
                   applsys.fnd_documents_short_text fdst,
                   po_headers_all y
             WHERE     1 = 1
                   AND fad.pk1_value(+) = y.po_header_id
                   AND fad.entity_name = 'PO_HEADERS'
                   AND fad.document_id = fd.document_id
                   AND fd.datatype_id = 1
                   and fad.seq_num>=100
                   AND fdst.media_id = fd.media_id
                   and y.type_lookup_code='STANDARD'
                   AND NVL(y.CANCEL_FLAG,'N')='N'
                   --and y.segment1 in (100,1000,100,650,26268)
                   --and y.segment1=1000
            UNION 
            SELECT poh.segment1, 1, '1'--null,null
              FROM    po.po_headers_all poh
                   LEFT JOIN
                      (SELECT fad1.pk1_value
                         FROM applsys.fnd_attached_documents fad1,
                              applsys.fnd_documents fd1
                        WHERE     1 = 1
                              AND fad1.entity_name = 'PO_HEADERS'
                              AND fad1.document_id = fd1.document_id
                              and fad1.seq_num>=100
                              AND fd1.datatype_id = 1) sub1
                   ON poh.po_header_id = sub1.pk1_value
             WHERE sub1.pk1_value IS NULL 
                        and poh.type_lookup_code='STANDARD'
                        AND NVL(poh.CANCEL_FLAG,'N')='N'
                      --  and poh.segment1 in (100,1000,100,650,26268)
                      --  and poh.segment1=1000
                      --   and poh.segment1=650)
È stato utile?

Soluzione

Usa parentesi. In questo momento, lo stai facendo ((set1 UNION set2) MINUS set1) UNION set2 Mentre intendevi fare (set1 UNION set2) MINUS (set1 UNION set2).

In altre parole, stai unendo set1 e set2, rimuovendo set1 da quello e unendo set2 con quello, mentre probabilmente hai intenzione di prendere un'unione di set1 e set2 e rimuovere un'unione di set1 e set2 da quello. UNION e MINUS avere la stessa precedenza e vengono elaborati nell'ordine che si incontrano.

Altri suggerimenti

Il problema è che, in primo luogo, fai un'unione delle prime due domande, quindi, fai un meno del terzo e poi sintonizzi il risultato con la quarta domanda.

Notare che UNION [ALL] e MINUS avere la stessa priorità. Essenzialmente, se ignoriamo i dettagli, stai facendo:

SELECT query1
UNION
SELECT query2
MINUS
SELECT query1
UNION
SELECT query2

Poiché tutti questi operatori hanno la stessa priorità, valutano uno per uno.SELECT query1 MINUS SELECT query2 UNION SELECT query1 dovrebbe tornare query2. Quindi, l'ultimo UNION viene applicato e il risultato è SELECT query2 UNION SELECT query2, che è ovviamente query2.

Per risolvere questo problema, dovresti fare qualcosa del genere:

SELECT * FROM (SELECT query1
               UNION
               SELECT query2)
MINUS
SELECT * FROM (SELECT query1
               UNION
               SELECT query2)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top