2008-10-28 20 views
7

Qualcuno sa cosa c'è di sbagliato in questa query?Ordine query PL/SQL Oracle Per problema con Distinct

SELECT DISTINCT c.CN as ClaimNumber, 
     a.ItemDate as BillReceivedDate, c.DTN as 
DocTrackNumber 
     FROM ItemData a, 
     ItemDataPage b, 
     KeyGroupData c 
     WHERE a.ItemTypeNum in (112, 113, 116, 172, 189) 
     AND a.ItemNum = b.ItemNum 
     AND b.ItemNum = c.ItemNum 
     ORDER BY a.DateStored DESC; 

ho fatto T-SQL maggior parte della mia carriera e questo è corretto per me, ma questa query è per un database Oracle e Toad solo posiziona il cursore sul a.DateStored nell'Ordine per sezione. Sono sicuro che questo è elementare per chiunque stia facendo PL/SQL.

Grazie!

[EDIT] Per riferimento futuro, l'errore di data da SQL * Plus è stata: "ORA-01791: non un'espressione selezionato"

risposta

15

Sarà necessario modificare la query in quanto tale:

SELECT DISTINCT c.CN as ClaimNumber, 
     a.ItemDate as BillReceivedDate, c.DTN as 
DocTrackNumber, a.DateStored 
     FROM ItemData a, 
     ItemDataPage b, 
     KeyGroupData c 
     WHERE a.ItemTypeNum in (112, 113, 116, 172, 189) 
     AND a.ItemNum = b.ItemNum 
     AND b.ItemNum = c.ItemNum 
     ORDER BY a.DateStored DESC; 

Quando si effettua una DISTINCT l'ordine da esigenze ad essere uno dei colonne selezionate.

2

Nevermind, l'esecuzione in SQL più mi ha dato una risposta più informativo. Il DateStored deve essere nell'istruzione SELECT in modo che questo funziona:

SELECT DISTINCT c.CN as ClaimNumber,   
a.ItemDate as BillReceivedDate, 
c.DTN as DocTrackNumber, 
a.DateStored   
FROM ItemData a,   
ItemDataPage b,   
KeyGroupData c   
WHERE a.ItemTypeNum in (112, 113, 116, 172, 189)   
AND a.ItemNum = b.ItemNum   
AND b.ItemNum = c.ItemNum   
ORDER BY a.DateStored DESC; 
+0

Proprio alla conclusione che sono venuto a ... – Carl

+0

Se si lascia questa domanda qui per riferimento futuro allora probabilmente dobbiamo notare che l'errore è data: ORA-01791: non è un'espressione selezionata – Carl

2

Credo che anche gli elementi della clausola order by debbano essere nella clausola select.

+2

Giusto per chiarire: Questo è vero a causa del DISTINCT. In molte query è possibile ordinare da una colonna che non si trova nell'elenco di selezione, ad es. SELECT name FROM emp ORDER BY empid. –