嗨,我正在尝试使以下逻辑正常工作。但它说不是按功能分组。.不知道为什么..我想获取两个表中的记录,然后将每个表的收入相加以在第一选择中累计总收入。
select ob_location_id,
ib_location_id,
vehicle_class,
return_date,
RENTAL_DATE,
sum(revenue) as TotalRevenue
from
(select ob_location_id,
ib_location_id,
vehicle_class,
return_date,
RENTAL_DATE,
SUM(DEMAND_TO_COME * BOARD_RATE) AS revenue
from PA_FCS_BLEND_FINAL) a
Union all
Select
ob_location_id,
ib_location_id,
vehicle_class,
return_date,
RENTAL_DATE,
sum(MATERIALIZED_BOH_REVENUE) as revenue
from PA_FCS_BLEND_BOH
group by
ob_location_id,
ib_location_id,
vehicle_class,
return_date,
RENTAL_DATE,
totalRevenue;
答案 0 :(得分:1)
a
内联视图缺少GROUP BY
子句GROUP BY
不应包含TOTAL_REVENUE
一个 dummy 示例(日期类型当然不匹配,但请不要在意):
SQL> CREATE TABLE pa_fcs_blend_final
2 (
3 ob_location_id NUMBER,
4 ib_location_id NUMBER,
5 vehicle_class NUMBER,
6 return_date NUMBER,
7 rental_date NUMBER,
8 demand_to_come NUMBER,
9 board_rate NUMBER
10 );
Table created.
SQL> insert into pa_fcs_blend_final values
2 (1, 2, 3, 4, 5, 6, 7);
1 row created.
SQL> CREATE TABLE pa_fcs_blend_boh
2 (
3 ob_location_id NUMBER,
4 ib_location_id NUMBER,
5 vehicle_class NUMBER,
6 return_date NUMBER,
7 rental_date NUMBER,
8 materialized_boh_revenue NUMBER
9 );
Table created.
SQL> insert into pa_fcs_blend_boh values
2 (1, 2, 3, 4, 5, 6);
1 row created.
查询返回某物;这是对的吗?不知道。
SQL> SELECT ob_location_id,
2 ib_location_id,
3 vehicle_class,
4 return_date,
5 rental_date,
6 SUM (revenue) AS totalrevenue
7 FROM ( SELECT ob_location_id,
8 ib_location_id,
9 vehicle_class,
10 return_date,
11 rental_date,
12 SUM (demand_to_come * board_rate) AS revenue
13 FROM pa_fcs_blend_final
14 GROUP BY ob_location_id, --> this is missing
15 ib_location_id,
16 vehicle_class,
17 return_date,
18 rental_date) a
19 GROUP BY ob_location_id,
20 ib_location_id,
21 vehicle_class,
22 return_date,
23 rental_date
24 UNION ALL
25 SELECT ob_location_id,
26 ib_location_id,
27 vehicle_class,
28 return_date,
29 rental_date,
30 SUM (materialized_boh_revenue) AS revenue
31 FROM pa_fcs_blend_boh
32 GROUP BY ob_location_id,
33 ib_location_id,
34 vehicle_class,
35 return_date,
36 rental_date --> total_revenue (you had) shouldn't be here
37 /
OB_LOCATION_ID IB_LOCATION_ID VEHICLE_CLASS RETURN_DATE RENTAL_DATE TOTALREVENUE
-------------- -------------- ------------- ----------- ----------- ------------
1 2 3 4 5 42
1 2 3 4 5 6
SQL>