2017-05-11 56 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' 
); 
相关问题