Sql 使用计数函数,但select语句中有多个字段以查看更多数据
我得到的错误是 '第2行错误ORA-00923:FROM关键字未在预期位置找到' 当我运行下面的查询时,我得到设备和它在第二个表(wo_work_order)中使用的次数,但我也需要第一个表(eq_位置)中的描述和标记的_as_deleted字段Sql 使用计数函数,但select语句中有多个字段以查看更多数据,sql,oracle,Sql,Oracle,我得到的错误是 '第2行错误ORA-00923:FROM关键字未在预期位置找到' 当我运行下面的查询时,我得到设备和它在第二个表(wo_work_order)中使用的次数,但我也需要第一个表(eq_位置)中的描述和标记的_as_deleted字段 除了在计数(工单设备)之前添加缺少的逗号,,,您还需要添加eq\u位置。说明和eq\u位置。在分组by子句中标记为已删除 select eq_locations.equipment, count(wo_work_order.equipment) fro
除了在计数(工单设备)之前添加缺少的逗号
,
,,您还需要添加eq\u位置。说明和eq\u位置。在分组by子句中标记为已删除
select eq_locations.equipment, count(wo_work_order.equipment) from eq_locations
left outer join wo_work_order
on eq_locations.equipment = wo_work_order.equipment
where eq_locations.plant='908'
group by eq_locations.equipment
order by eq_locations.equipment
对联接使用JOIN ON
语法。不要使用a,b
语法。此外,更喜欢使用简单的表别名,而不是到处使用整个表名。我想您需要使用LEFT JOIN
进行第二次查询,以获得工单
表中缺失设备的0计数。这个查询应该可以正常工作
select
eq_locations.equipment,eq_locations.description,
eq_locations.marked_as_deleted, count(wo_work_order.equipment)
FROM
eq_locations, wo_work_order
where
eq_locations.equipment=wo_work_order.equipment and
eq_locations.plant='908' and eq_locations.marked_as_deleted = '0'
group by
eq_locations.equipment, eq_locations.description, eq_locations.marked_as_deleted
order by
eq_locations.equipment
缺少,
在标记为已删除计数(wo
)之间?
select
eq_locations.equipment,eq_locations.description,
eq_locations.marked_as_deleted, count(wo_work_order.equipment)
FROM
eq_locations, wo_work_order
where
eq_locations.equipment=wo_work_order.equipment and
eq_locations.plant='908' and eq_locations.marked_as_deleted = '0'
group by
eq_locations.equipment, eq_locations.description, eq_locations.marked_as_deleted
order by
eq_locations.equipment
SELECT e.equipment,
e.description,
e.marked_as_deleted,
COUNT(w.equipment)
FROM eq_locations e LEFT OUTER JOIN wo_work_order w
ON e.equipment=w.equipment
AND e.plant='908'
AND e.marked_as_deleted = '0'
GROUP BY e.equipment,
e.description,
e.marked_as_deleted
ORDER BY e.equipment;