SQL查询以订购列的总和

发布于 2025-01-28 08:05:51 字数 892 浏览 3 评论 0原文

我有以下表格:

驱动程序表,带有驱动程序_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谜泪 2025-02-04 08:05:51

我终于设法做到了。这是工作查询:

select 
  Driver_Code, 
  SUM(km) as Total_km 
from 
  Route_files 
  inner join Routes on Route_files.Route_Code = Routes.Route_Codes 
GROUP BY 
  Route_files.Driver_Code 
ORDER BY 
  Total_km Descending

最初,我在做选择driver_code,km,sum(km),并且在尝试通过组进行组时,dbase强迫我按driver_code和km进行组,这意味着这意味着总和函数被应用于每个条目,而不是在单个驱动程序_Code的所有条目上,这是我需要的。现在,我终于明白了哪个组!

感谢大家的评论!

I finally managed to do it. This is the working query:

select 
  Driver_Code, 
  SUM(km) as Total_km 
from 
  Route_files 
  inner join Routes on Route_files.Route_Code = Routes.Route_Codes 
GROUP BY 
  Route_files.Driver_Code 
ORDER BY 
  Total_km Descending

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文