SQL查询以订购列的总和
我有以下表格:
驱动程序表,带有驱动程序_code列
Route_files表,带驱动程序_code列和一个route_code列
路由表,带Route_code列和一个公里列的
每个条目中的每个条目中的每个条目都可能有超过1个条目带有相同驱动程序的Route_files表。对于Route_files表中的每个条目,路由表中只有一个条目,带有相同的Route_code。
我要做的是根据他们驾驶的公里数订购驾驶员。因此,如果我有以下数据:
驱动程序:
Driver_Code
2
3
4
route_files:
Driver_Code Route_Code
2 20
2 50
2 30
3 30
4 40
路线:
Route_Code Kilometers
20 1231
30 9
40 400000
50 24234
然后驱动程序2驱动路线20 30和50,因此总公里为25474。类似地,驾驶员3驱动器3驱动9公里,驱动程序4驱动400000。我需要的SQL查询应输出:
Driver_Code Total_km
4 400000
2 25474
3 9
我试图在Route_files和路由表上使用内部连接,以获取具有所有必要信息的单个“表”,希望我可以进一步使用此获得的表,但无法弄清楚如何做。我在DBASE 2019中工作(不幸的是,无法更改为更好的事情)。任何提示和想法都将受到赞赏!
I have the following tables:
Drivers table, with a Driver_Code column
Route_files table, with a Driver_Code column and a Route_Code column
Routes table, with a Route_Code column and a Kilometers column
For every entry in the Drivers table there may be more than 1 entry in the Route_files table with the same Driver_Code. For every entry in the Route_files table, there is only one entry in the Routes table with the same Route_Code.
What I am trying to do is order the Drivers based on the total number of kilometers that they drove. So if I have the following data:
Drivers:
Driver_Code
2
3
4
Route_files:
Driver_Code Route_Code
2 20
2 50
2 30
3 30
4 40
Routes:
Route_Code Kilometers
20 1231
30 9
40 400000
50 24234
Then Driver 2 drove routes 20 30 and 50 so the total kilometers is 25474. Similarly driver 3 drove 9km and driver 4 drove 400000. The SQL query that I need should output:
Driver_Code Total_km
4 400000
2 25474
3 9
I tried to use an inner join on the Route_files and Routes tables to obtain a single "table" with all the necessary information, hoping that I could further use this obtained table, but couldn't figure out how to do that. I am working in dBase 2019(and can't change to something better, unfortunately). Any hints and ideas are appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于设法做到了。这是工作查询:
最初,我在做
选择driver_code,km,sum(km)
,并且在尝试通过组进行组时,dbase强迫我按driver_code和km进行组,这意味着这意味着总和函数被应用于每个条目,而不是在单个驱动程序_Code的所有条目上,这是我需要的。现在,我终于明白了哪个组!感谢大家的评论!
I finally managed to do it. This is the working query:
Initially I was doing
select Driver_Code, km, SUM(km)
and when trying to do GROUP BY, dBase was forcing me to group by Driver_Code as well as km, which meant that the SUM function was being applied to every single entry instead of on all the entries of a single Driver_Code, which is what I needed. Now I finally understand what GROUP BY does!Thanks everyone for your comments!