2017-12-14 3 views
-3

Dies ist meine Abfrage. Ich möchte Unterschiede zwischen den Preisen an verschiedenen Daten zeigen.Sql und Orakel Apex, "einzelne Zeile Unterabfrage gibt mehr als eine Zeile zurück" -Fehler

select 
     id, 
     PRODUCT, 
     PRICE, 
     P_DATE, 
(SELECT(t2.price - t1.price) AS PRICE_DIFF 
FROM PRODUCT_TABLE t1 CROSS JOIN 
    PRODUCT_TABLE t2 
WHERE t1.p_date = '11-DEC-17' AND t2.p_date = '12-DEC-17' AND t1.id=t2.id) as PRICE_DIFF 
    from PRODUCT_TABLE 

Dies ist der Fehler, den ich bekommen:

ORA-01427: einreihige Unterabfrage gibt mehr als eine Zeile

Wie kann ich diesen Fehler vermeiden?

+2

Bitte beachten Sie, dass Links verloren gehen können, so dass diese Frage nutzlos für zukünftige Nutzer. Aus diesem Grund werden viele Leser die Frage off-topic für den Inhalt dessen betrachten, was Sie hier gepostet haben. Was ist weitgehend nichts. – Yunnosch

+0

bitte gehen Sie bitte zum 'wählen -ID ändern, PRODUCT, PREIS, P_DATE, (SELECT max (t2.price - t1.price) AS nav_return VON pivot_table t2 WHERE t1.p_date = '11 -DEC- 17 'UND t2.p_date = '12 -DEC-17' UND t1.id = t2.id) als nav_retun von PIVOT_TABLE t1' gerne das Ergebnis – Rams

+0

Könnten Sie Ihre Tabellenstruktur kopieren? –

Antwort

0

Funktioniert so etwas für Sie?

Setup:

with product_table as 
(
select 1 as id, 'cheese' as product, 1000 as price, TO_DATE('11-DEC-17', 'DD-MON-YY') as p_date from dual 
union all 
select 1, 'cheese', 1100, TO_DATE('12-DEC-17', 'DD-MON-YY') from dual 
union all 
select 2, 'onion', 500, TO_DATE('11-DEC-17', 'DD-MON-YY') from dual 
union all 
select 2, 'onion', 550, TO_DATE('12-DEC-17', 'DD-MON-YY') from dual 
) 

Abfrage:

select 
    t1.id, 
    t1.product, 
    t1.price, 
    t1.p_date, -- maybe t2.p_date? not sure which is relevant 
    t2.price - t1.price as price_diff 
from 
    product_table t1 
inner join 
    product_table t2 
on t1.id = t2.id 
where 
    t1.p_date = '11-DEC-17' 
and t2.p_date = '12-DEC-17' 

Ausgang:

 ID PRODUCT  PRICE P_DATE PRICE_DIFF 
---------- ------- ---------- --------- ---------- 
     1 cheese  1000 11-DEC-17  100 
     2 onion   500 11-DEC-17   50 
Verwandte Themen