您好我最近开始自学Oracle SQL,我遇到了以下问题。如果能引导我如何处理这类问题的解决方案,我将很高兴。
我们有一个数据库。 数据库由以下表组成:
表route_info包含单轨铁路用于行驶的路线的描述。
route_info(route_no INTEGER, source STRING, destination STRING, length INTEGER, departs TIME, arrives TIME, cost INTEGER);
+----------+----------+-------------+--------+----------+----------+------+
| route_no | source | destination | length | departs | arrives | cost |
+----------+----------+-------------+--------+----------+----------+------+
| 1462 | Banglore | Delhi | 1516 | 13:00:00 | 15:00:00 | 4500 |
| 4456 | Gwalior | Delhi | 543 | 05:05:06 | 04:55:24 | 546 |
| 4524 | Banglore | Delhi | 1516 | 04:44:00 | 13:00:45 | 1200 |
| 7490 | Banglore | Gwalior | 1343 | 01:16:17 | 17:07:08 | 1400 |
| 7890 | Agra | Gwalior | 343 | 01:15:41 | 07:07:08 | 400 |
+----------+----------+-------------+--------+----------+----------+------+
表单轨包含有关每条单轨列车的信息。
monorail(rid INTEGER, rname STRING, runningrange INTEGER);
+------+---------------+--------------+
| rid | rname | runningrange |
+------+---------------+--------------+
| 4454 | Mark Jung Exp | 300 |
| 1234 | Speedester | 454 |
| 2134 | Monstor | 4054 |
| 7334 | Father | 1051 |
| 4444 | Supreme | 3023 |
| 1444 | Grain | 3023 |
+------+---------------+--------------+
表员工包含组织中所有员工的信息(包括单轨列车司机)。
employee(eid INTEGER, ename STRING, salary INTEGER);
+------+--------+--------+
| eid | ename | salary |
+------+--------+--------+
| 101 | Nusrat | 100 |
| 234 | emp1 | 5050 |
| 323 | emp2 | 7050 |
| 314 | emp3 | 10050 |
| 541 | emp4 | 9010 |
| 432 | emp5 | 12345 |
| 123 | Kabir | 400 |
| 555 | Raheem | 235 |
+------+--------+--------+
分配的表包含哪些员工驾驶单轨列车的数据。这可以是多对多的关系。
assigned(eid INTEGER, rid INTEGER);
+------+------+
| eid | rid |
+------+------+
| 101 | 4454 |
| 234 | 1234 |
| 323 | 1234 |
| 314 | 1234 |
| 314 | 2134 |
| 541 | 2134 |
| 123 | 7334 |
| 101 | 4444 |
| 101 | 1444 |
+------+------+
我们获得以下指导
现在我们需要为此编写一个Oracle SQL查询: 对于“运行范围”大于500的所有单轨道路,找到轨道的id(“rid”)和分配给该轨道的所有司机的工资总和。
答案 0 :(得分:2)
将其分解为较小的部分将使其更容易理解:
首先获得所有单轨列车的运行范围"大于500:
SELECT rid
FROM monorail
WHERE runningrange > 500
然后将inner join
添加到assigned
表:
SELECT m.rid
FROM monorail m
INNER JOIN assigned a ON(m.rid = a.rid)
WHERE runningrange > 500
然后将另一个inner join
添加到employee
表:
SELECT m.rid
FROM monorail m
INNER JOIN assigned a ON(m.rid = a.rid)
INNER JOIN employee e ON(a.eid = e.eid)
WHERE runningrange > 500
最后添加group by
和sum
。
SELECT m.rid, SUM(salary)
FROM monorail m
INNER JOIN assigned a ON(m.rid = a.rid)
INNER JOIN employee e ON(a.eid = e.eid)
WHERE runningrange > 500
GROUP BY m.rid
ORDER BY SUM(salary)