有关列问题的内部联接表

时间:2019-03-22 17:02:51

标签: sql oracle

下面是我的查询,由于某种原因,它保持运行并且不返回任何内容。我想内部连接两个表并添加条件booking_value = 1(恰好在我要连接的表中)。我希望显示所有与第二张表具有相同租期的记录,但预订_value = 1的记录。

select a.timestamp , SUM(DEMAND_TO_COME * BOARD_RATE) AS 
RevenueTotal, 
count(DEMAND_TO_COME * BOARD_RATE) as NumofTrans from(
select  PA_FCS.ob_location_id, 
PA_FCS.ib_location_id, 
PA_FCS.vehicle_class, 
PA_FCS.return_date,
PA_FCS.RENTAL_DATE,board_rate,
PA_FCS.Demand_to_come,
substr(PA_FCS.rental_date, 0,8) as timestamp
from PA_FCS 

inner join pa_reservation on pa_reservation.Rental_date = 
PA_FCS.rental_date where booking_value=1
) a
group by a.timestamp
order by a.timestamp;

这应该可以工作,并为我提供以下三列

 Timestamp    RevenueTotal       NumofTrans
 20190220      51435.56745        123665

不确定在这里我在做什么错。

1 个答案:

答案 0 :(得分:2)

您可以像这样简化查询(不需要子查询及其投影中的某些字段):

SELECT
    substr(pa_fcs.rental_date, 0, 8) AS timestamp,
    SUM(pa_fcs.demand_to_come * board_rate) AS revenuetotal,
    COUNT(*) AS numoftrans
FROM
    pa_fcs
    INNER JOIN pa_reservation ON pa_reservation.rental_date = pa_fcs.rental_date
WHERE
    booking_value = 1
GROUP BY
    substr(pa_fcs.rental_date, 0, 8)
ORDER BY
    timestamp;

我不确定您要使用count(DEMAND_TO_COME * BOARD_RATE)实现什么。如果您希望count(*)的记录数。