我是SQL的新手,我试图弄清楚为什么我会收到错误。我试图在查询中添加2列。
以下是原始代码:
select f.id ride_id,f.driver_id,f.fleetid,f.fare,f.tip,f.service_fee,(f.fare+f.tip+f.service_fee) total,f.pick_up_city,f.pick_up_state,f.created_at
from
(select r.id,ra.driver_id,r.created_at,r.pick_up_city,r.pick_up_state,ra.xid fleetid,
MAX(CASE WHEN cli.category='fare' THEN cli.amount ELSE NULL END)/100 fare,
MAX(CASE WHEN cli.category='gratuity' THEN cli.amount ELSE NULL END)/100 tip,
MAX(CASE WHEN cli.category='flywheel_service_fee' THEN cli.amount ELSE NULL END)/100 service_fee
from rides r,charges c,charge_line_items cli,(select distinct ri.id,h.driver_id,f.xid
from rides ri inner join hails h on ri.id=h.ride_id
inner join vehicles v on h.vehicle_id=v.id
inner join fleets f on v.fleet_id=f.id
where ri.status IN ('completed', 'waiting_for_payment_data', 'waiting_for_payment_instrument', 'processing_payment', 'payment_pending')
and h.status='completed' and ri.created_at between '2015-07-15 04:00:00' and '2015-09-11 04:00:00'
and f.xid in ('10202')) ra
where r.id=ra.id and r.id=c.ride_id and c.id=cli.charge_id
group by r.id,ra.xid,ra.driver_id) f
我试图找到驱动程序first_name和last_name。我在h.driver_id :: VARCHAR = dr.xid中添加了INNER JOIN驱动程序AS dr,在SELECT子句中添加了dr.first_name,dr.last_name。 (我把星号放在我添加的部分周围。)
select f.id ride_id,f.driver_id,**dr.first_name,dr.last_name,**f.fleetid,f.fare,f.tip,f.service_fee,(f.fare+f.tip+f.service_fee) total,f.pick_up_city,f.pick_up_state,f.created_at
from
(select r.id,ra.driver_id,r.created_at,r.pick_up_city,r.pick_up_state,ra.xid fleetid,
MAX(CASE WHEN cli.category='fare' THEN cli.amount ELSE NULL END)/100 fare,
MAX(CASE WHEN cli.category='gratuity' THEN cli.amount ELSE NULL END)/100 tip,
MAX(CASE WHEN cli.category='flywheel_service_fee' THEN cli.amount ELSE NULL END)/100 service_fee
from rides r,charges c,charge_line_items cli,(select distinct ri.id,h.driver_id,f.xid
from rides ri inner join hails h on ri.id=h.ride_id
inner join vehicles v on h.vehicle_id=v.id
inner join fleets f on v.fleet_id=f.id
**inner join drivers AS dr on h.driver_id::VARCHAR = dr.xid**
where ri.status IN ('completed', 'waiting_for_payment_data', 'waiting_for_payment_instrument', 'processing_payment', 'payment_pending')
and h.status='completed' and ri.created_at between '2015-07-15 04:00:00' and '2015-09-11 04:00:00'
and f.xid in ('10202')) ra
where r.id=ra.id and r.id=c.ride_id and c.id=cli.charge_id
group by r.id,ra.xid,ra.driver_id) f
当我运行此查询时,我收到错误说"缺少表dr"的FROM子句条目。通常情况下,我可以加入我想要SELECT的表,但由于某种原因,查询中有一些东西不允许我这样做。
非常感谢任何帮助。我还在学习,并且想要弄清楚我做错了什么。
谢谢!
答案 0 :(得分:1)
您已在第二级子查询中添加了JOIN
。这很好,但您必须使用分配给子查询的别名将第二级子查询的选择列表中的两列“传播”到主级别。正确概述代码会使错误和解决方案变得明显:
SELECT f.id AS ride_id, f.driver_id,
f.first_name, f.last_name,
f.fleetid, f.fare, f.tip, f.service_fee, (f.fare+f.tip+f.service_fee) total,
f.pick_up_city, f.pick_up_state, f.created_at
FROM (
-- First sub-query starting
SELECT r.id, ra.driver_id, r.created_at, r.pick_up_city, r.pick_up_state, ra.xid AS fleetid,
ra.first_name, ra.last_name,
max(CASE WHEN cli.category = 'fare' THEN cli.amount ELSE NULL END)/100 fare,
max(CASE WHEN cli.category = 'gratuity' THEN cli.amount ELSE NULL END)/100 tip,
max(CASE WHEN cli.category= 'flywheel_service_fee' THEN cli.amount ELSE NULL END)/100 service_fee
FROM rides r, charges c, charge_line_items cli, (
-- Second sub-query starting
SELECT DISTINCT ri.id, h.driver_id, f.xid, dr.first_name, dr.last_name
FROM rides ri
INNER JOIN hails h ON ri.id = h.ride_id
INNER JOIN vehicles v ON h.vehicle_id = v.id
INNER JOIN fleets f ON v.fleet_id = f.id
INNER JOIN drivers dr ON h.driver_id::varchar = dr.xid
WHERE ri.status IN ('completed', 'waiting_for_payment_data', 'waiting_for_payment_instrument', 'processing_payment', 'payment_pending')
AND h.status = 'completed'
AND ri.created_at BETWEEN '2015-07-15 04:00:00' AND '2015-09-11 04:00:00'
AND f.xid IN ('10202')) ra
-- End of second sub-query
WHERE r.id = ra.id
AND r.id = c.ride_id
AND c.id = cli.charge_id
-- The below GROUP BY clause is incomplete, see added line
GROUP BY r.id, ra.xid, ra.driver_id
, r.created_at, r.pick_up_city, r.pick_up_state, ra.first_name, ra.last_name) f
-- End of first sub-query
;
否则原始查询是编码样式的奇怪组合,例如 - 两个样式用于列出多个表(逗号分隔和JOIN
子句),奇数大写和 - 正如您刚刚学到的 - 蹩脚概述。好像你不是唯一一个有学习东西的人......