2017-05-11 57 views
0

是否有任何其他方式來編寫此查詢,以便它不會得到錯誤?如何處理返回多個值錯誤的子查詢?

select sum(Travelled_value) 
from travel_table 
where customer_id=(select distinct f.CUSTOMER_ID as agg 
        from SEGMENT_table f 
        JOIN bookin_table t 
        ON f.CUSTOMER_ID=t.CUSTOMER_ID 
        where t.booking_date BETWEEN sysdate 
        AND sysdate+21 and f.type='NEW';) 

這裏有三個表,其中customer_id是通用的。

+0

編輯您的問題並顯示錯誤。 –

+1

也許你想'IN'子查詢,而不是'='子查詢。 – jarlh

+0

用'where customer_id in'替換'where customer_id =' – dasblinkenlight

回答

1

我不知道這是否會工作,但它修正了許多問題:

select sum(tt.Travelled_value) 
from travel_table tt 
where tt.customer_id in (select f.CUSTOMER_ID 
         from SEGMENT_table f JOIN 
           booking_table t 
           ON f.CUSTOMER_ID = t.CUSTOMER_ID 
         where t.booking_date between sysdate and sysdate+21 and 
           f.type = 'NEW' 
         ); 

注:

  • 您在查詢中間的分號。它結束了。
  • select distinctin子查詢中不需要。
  • 您正在使用sysdate並將其與日期進行比較。你確定你不想要trunc(sysdate)sysdate有一個時間組件。
0
SELECT SUM(Travelled_value) 
FROM travel_table 
WHERE customer_id in 
    (SELECT f.CUSTOMER_ID 
    FROM SEGMENT_table f 
    JOIN bookin_table t 
    ON f.CUSTOMER_ID=t.CUSTOMER_ID 
    WHERE t.booking_date BETWEEN trunc(sysdate) AND trunc(sysdate+21) 
    AND f.type='NEW' 
); 
相關問題