正在努力选择SQL子查询

正在努力选择SQL子查询,sql,Sql,为了修改的目的,我试图回答一个SQL问题,但似乎不知道如何让它工作。有关表格如下: 问题是要我编写一个SQL命令,为每个距离所有行程超过100英里的员工显示该员工的姓名以及该员工在所有行程中使用的总升数。一次行程的升数为distanceInKm/km/l 到目前为止,我已经尝试了以下几种代码变体: SELECT name, TravelCost.distanceInKm / Car.kmPerLitre AS "Cost in Litres" FROM Employee, C

为了修改的目的,我试图回答一个SQL问题,但似乎不知道如何让它工作。有关表格如下:

问题是要我编写一个SQL命令,为每个距离所有行程超过100英里的员工显示该员工的姓名以及该员工在所有行程中使用的总升数。一次行程的升数为distanceInKm/km/l

到目前为止,我已经尝试了以下几种代码变体:

SELECT 
    name, TravelCost.distanceInKm / Car.kmPerLitre AS "Cost in Litres"
FROM 
    Employee, Car, TravelCost
WHERE 
    Employee.id = TravelCost.employeeID
    AND Car.regNo = TravelCost.carRegNo

在这一点上,我有点卡住了,任何帮助都将不胜感激,谢谢

使用GROUPBY和Having子句

SELECT NAME,
       Sum(TravelCost.distanceInKm/ Car.kmPerLitre) AS "Cost in Litres"
FROM   Employee
       INNER JOIN TravelCost
               ON Employee.id = TravelCost.employeeID
       INNER JOIN Car
               ON Car.regNo = TravelCost.carRegNo
GROUP  BY NAME
HAVING Sum(distanceInKm) > 100 
请勿在FROM子句中使用逗号。始终使用正确、标准、明确的联接语法

您缺少一个组,并且有:


您需要联接所有表,并按如下方式查找升的总和:

select
    e.*,
    sum(distanceInKm/c.kmPerLitre) litres
from employee e
inner join travelcost t
on e.id = t.employeeId
inner join car c
on t.carRegNo = c.regNo
group by e.id, e.name
having sum(t.distanceInKm) > 100;
此外,您需要按id分组,而不是像其他答案所建议的那样仅按姓名分组。可以有多个同名员工


此外,请使用显式连接语法,而不是旧的基于逗号的语法。它既现代又清晰。

我删除了不兼容的数据库标签。请标记您真正使用的数据库。-25年前,在ANSI-92 SQL标准中,旧样式的逗号分隔表列表样式被正确的ANSI连接语法所取代,它的使用是不鼓励的谢谢,工作非常完美,我以前也尝试过类似的方法,但在SELECT子句中忽略了总和。如果我们使用CTE或apply将条件的分组数据连接起来,则性能更好
-- **How fool am I! How arrogant am I! I just thought `sum(tc.distanceInKm/c.kmPerLitre)` 
-- may have a problem, since a employee may have multiple cars,and car's kmPerLitre is differenct. 
-- However there is no problem, it's simple and right! 
-- The following is what I wrote, what a bloated statement it is! **

-- calcute the total number of litres used by the employee on all journeys
select  e.name, sum(Cost_in_Litres) as "Cost in Litres"
from (
    select t.employeeID
        -- calcute the litres used by the employee on all journeys group by carRegNo
        , sum(t.distanceInKm)/avg(c.kmPerLitre) as Cost_in_Litres
    from TravelCost t
    inner join Car c
        on c.regNo = t.carRegNo
    where t.employeeID in 
    ( -- find the employees who has a total distance from all journeys of more than 100
    select employeeID 
        from TravelCost
        group by employeeID 
        having sum(distanceInKm)> 100
    )
    group by t.carRegNo, t.employeeID
) a
inner join Employee e
    on e.id = a.employeeID
group by e.id,e.name;
-- **How fool am I! How arrogant am I! I just thought `sum(tc.distanceInKm/c.kmPerLitre)` 
-- may have a problem, since a employee may have multiple cars,and car's kmPerLitre is differenct. 
-- However there is no problem, it's simple and right! 
-- The following is what I wrote, what a bloated statement it is! **

-- calcute the total number of litres used by the employee on all journeys
select  e.name, sum(Cost_in_Litres) as "Cost in Litres"
from (
    select t.employeeID
        -- calcute the litres used by the employee on all journeys group by carRegNo
        , sum(t.distanceInKm)/avg(c.kmPerLitre) as Cost_in_Litres
    from TravelCost t
    inner join Car c
        on c.regNo = t.carRegNo
    where t.employeeID in 
    ( -- find the employees who has a total distance from all journeys of more than 100
    select employeeID 
        from TravelCost
        group by employeeID 
        having sum(distanceInKm)> 100
    )
    group by t.carRegNo, t.employeeID
) a
inner join Employee e
    on e.id = a.employeeID
group by e.id,e.name;