我正在尝试运行以下代码,但由于SELECT语句中的最后一个字段同时包含汇总条件和正常条件而收到错误
trunc(d.ead_date)-trunc(max(d.pickup_date_ts))AS diff_ead_cpt_max
错误基本上是说GROUP BY子句中不允许聚合。如果我删除最大值,则可以使用,但是无法获得正确的结果。
SELECT
w1.physical_country origin_country,
a.leg_warehouse_id lm_warehouse_id,
b.leg_warehouse_id fl_warehouse_id,
c.plane_name,
k.leg_ware,
k.last_ds,
trunc(a.ead_date) ead_date,
max(d.pickup_date_ts) max_cpt,
to_char(max(d.pickup_date_ts),'HH24:MI') max_cpt_time,
trunc(max(d.pickup_date_ts)) max_cpt_date,
trunc(d.ead_date)- trunc(max(d.pickup_date_ts)) AS diff_ead_cpt_max
from
final_leg a
inner join dest_leg b
on a.shipment_id = b.shipment_id and a.route_id = b.route_id
inner join sc_execution_eu.o_detailed_routes_v2 d
on a.shipment_id = d.shipment_id and a.route_id = d.route_id and d.leg_sequence_id = 0
left join plane_leg c
on a.shipment_id = c.shipment_id and a.route_id = c.route_id
left join warehouse_attributes w1
on a.route_warehouse_id = w1.warehouse_id
left join warehouse_attributes w2
on b.leg_warehouse_id = w2.warehouse_id
RIGHT JOIN list_legs_ds k
on a.route_warehouse_id = k.leg_ware AND a.leg_warehouse_id = k.last_ds
group by
1,2,3,4,5,6,7,11
答案 0 :(得分:1)
GROUP BY
非汇总值,并使用列名:
SELECT
w1.physical_country origin_country,
a.leg_warehouse_id lm_warehouse_id,
b.leg_warehouse_id fl_warehouse_id,
c.plane_name,
k.leg_ware,
k.last_ds,
trunc(a.ead_date) ead_date,
max(d.pickup_date_ts) max_cpt,
to_char(max(d.pickup_date_ts),'HH24:MI') max_cpt_time,
trunc(max(d.pickup_date_ts)) max_cpt_date,
trunc(d.ead_date)- trunc(max(d.pickup_date_ts)) AS diff_ead_cpt_max
from
final_leg a
inner join dest_leg b
on a.shipment_id = b.shipment_id and a.route_id = b.route_id
inner join sc_execution_eu.o_detailed_routes_v2 d
on a.shipment_id = d.shipment_id and a.route_id = d.route_id and d.leg_sequence_id = 0
left join plane_leg c
on a.shipment_id = c.shipment_id and a.route_id = c.route_id
left join warehouse_attributes w1
on a.route_warehouse_id = w1.warehouse_id
left join warehouse_attributes w2
on b.leg_warehouse_id = w2.warehouse_id
RIGHT JOIN list_legs_ds k
on a.route_warehouse_id = k.leg_ware AND a.leg_warehouse_id = k.last_ds
group by
w1.physical_country origin_country,
a.leg_warehouse_id lm_warehouse_id,
b.leg_warehouse_id fl_warehouse_id,
c.plane_name,
k.leg_ware,
k.last_ds,
trunc(a.ead_date),
trunc(d.ead_date)
答案 1 :(得分:1)
您不能删除您已经说过的max
,因为pickup_date_ts
是未汇总的列,未列在GROUP BY
列表中。
尝试trunc( max(trunc(d.ead_date) - d.pickup_date_ts))
。
顺便说一句,不建议在GROUP BY
表达式中使用列顺序号,SELECT
列表中的某些更改可能会对它们产生不利影响,而应明确地写出列名。