Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 单独select语句旁边两列的总和_Sql_Sqlite - Fatal编程技术网

Sql 单独select语句旁边两列的总和

Sql 单独select语句旁边两列的总和,sql,sqlite,Sql,Sqlite,我正在处理一个SQL任务,我不知道如何在显示另一个表中的信息时从同一个表中获取两列之和 我已经尝试了多种方法,大概花了两个小时来解决这个问题 我有两张表:员工表和燃料表。我显示了员工的所有信息。我必须做的第一个SQL语句: SELECT firstname, lastname, title, registrationyear, make, model FROM Employees ORDER BY make; MyEmployees表有以下列:firstname、lastname、employ

我正在处理一个
SQL任务
,我不知道如何在显示另一个表中的信息时从同一个表中获取两列之和

我已经尝试了多种方法,大概花了两个小时来解决这个问题

我有两张表:
员工表和燃料表。我显示了员工的所有信息。我必须做的第一个SQL语句:

SELECT firstname, lastname, title, registrationyear, make, model FROM Employees ORDER BY make;
My
Employees表有以下列:
firstname、lastname、employeeid、make、model、registrationyear、title

我的
燃油表
有以下列:
当前价格、燃油类型、燃油成本、里程数、里程数、燃油金额、员工ID、日期

我的说明是:“一个显示员工当前使用的汽车的列表(我做的第一个SQL语句,所以这个就完成了!) 与上述报告一样,还包括员工行驶的总公里数和总燃油成本。”(这是我试图陈述的任务)

我尝试过使用
如,UNION,UNION ALL
等。我所能做的最好的事情是在信息的顶部列出员工信息和总数,而不是在查询中的其他数据旁边列出他们自己的两列


我真的被困在这里了。有人能帮我吗?

您可以使用下面提到的内部join&Group By子句。如果你有别的意思,请告诉我

SELECT A.firstname, A.lastname, A.title, A.registrationyear, A.make, A.model,
SUM(B.Column_Having_Kilometer_Driven_Value)
FROM
Employee A
INNER JOIN Fuel B ON A.EmployeeID = B.EmployeeID
Group By A.EmployeeID, A.firstname, A.lastname, A.title, A.registrationyear, A.make, A.model

第二项任务比第一项任务更复杂

首先,将两个或多个表中的列合并在一行中是
join
的目的,因此您必须基于
employeeid
来联接这两个表。这会还给你一张这样的桌子

employeeid | other emp fields | fuel date  | other fueld fields
1          | ...              | 01/01/2017 | ...
1          | ...              | 01/02/2017 | ...
2          | ...              | 01/01/2017 | ...
2          | ...              | 02/01/2017 | ...
2          | ...              | 04/03/2017 | ...
select  t1.firstname, t1.lastname, t1.title, t1.registrationyear, t1.make, t1.model,
        sum(t2.mileage) as total_milege,
        sum(t2.fuelcost * t2.fuelamount) as total_fuel_cost
from    Employees t1
join    Fuel t2
on      t1.employeeid = t2.employeeid
group by t1.firstname, t1.lastname, t1.title, t1.registrationyear, t1.make, t1.model
从这里,您需要来自每个
员工的数据与来自
燃料
的与该
员工相关的行的总和相结合,这就是
分组依据
的目的

使用
分组依据
时,定义一组列,定义分组标准;select语句中的所有其他内容都必须以某种方式分组(在使用
sum
的情况下),以便
group by
中的列保持唯一

您的最终查询如下所示

employeeid | other emp fields | fuel date  | other fueld fields
1          | ...              | 01/01/2017 | ...
1          | ...              | 01/02/2017 | ...
2          | ...              | 01/01/2017 | ...
2          | ...              | 02/01/2017 | ...
2          | ...              | 04/03/2017 | ...
select  t1.firstname, t1.lastname, t1.title, t1.registrationyear, t1.make, t1.model,
        sum(t2.mileage) as total_milege,
        sum(t2.fuelcost * t2.fuelamount) as total_fuel_cost
from    Employees t1
join    Fuel t2
on      t1.employeeid = t2.employeeid
group by t1.firstname, t1.lastname, t1.title, t1.registrationyear, t1.make, t1.model

注意:我不知道
里程数
里程数
之间的区别,因此我的查询中涉及这些字段的部分可能需要进行一些调整。

我不知道Employees表下的总和部分是什么。我尝试重新安排:“从Employees内部选择名字、名字、头衔、注册年份、品牌、型号加入Employees上的Fuel.EmployeeID=Fuel.EmployeeID按雇员分组。EmployeeID、名字、姓氏、头衔、注册年份、品牌、型号;”我得到的输出基本上与第一个任务中的第一条SQL语句相同,而Fuel表中没有任何输出。另外,我还需要一个列来记录燃料的总成本。我爱你。我确实学到了一些东西!它工作得很好!很高兴它有帮助!如果这个或任何答案已经解决了你的问题,请考虑点击复选标记。这向更广泛的社区表明,你已经找到了一个解决方案,并给回答者和你自己带来了一些声誉。没有义务这样做。