我有3张桌子:钱,学生,教师。该查询返回每个系中的每个系和最高津贴。
select
f.name as "FACULTY_NAME",
max(stipend) as "MAX_STIPEND"
from
money m, student s
inner join
faculty f on f.id_faculty = s.faculty_id
where
m.student_id = s.id_student
group by
f.id_faculty, f.name;
查询工作正常:
FACULTY_NAME | MAX_STIPEND
-----------------+---------------
IT Faculty | 50
Architecture | 60
Journalism | 40
但是,当我在原始查询中添加s.name来显示接收max_stipend的学生的姓名时,查询也无法像以前那样工作-它会返回所有学生
select
f.name as "FACULTY_NAME",s.name,
max(stipend) as "MAX_STIPEND"
from
money m, student s
inner join
faculty f on f.id_faculty = s.faculty_id
where
m.student_id = s.id_student
group by
f.id_faculty, f.name, s.name;
查询结果:
FACULTY_NAME | s.name | MAX_STIPEND
----------------+-----------+---------------
IT Faculty | Joe | 50
IT Faculty | Lisa | 10
Architecture | Bob | 60
Journalism | Fred | 5
Architecture | Susan | 5
Journalism | Tom | 40
使用右,左和内联接执行相同的操作。有人可以告诉问题出在哪里吗?
答案 0 :(得分:1)
首先,您应该对所有所有联接使用正确的JOIN
语法。
第二,您可以使用Oracle的keep
语法:
select f.name as FACULTY_NAME,
max(stipend) as MAX_STIPEND,
max(s.name) keep (dense_rank first order by stipend desc)
from money m join
student s
on m.student_id = s.id_student join
faculty f
on f.id_faculty = s.faculty_id
group by f.id_faculty, f.name;
答案 1 :(得分:0)
但是,当我在原始查询中添加s.name来显示接收max_stipend的学生的姓名时,查询也无法像以前那样工作-它会返回所有学生
添加s.name
时,您正在寻找每个用户的最小值。
如果您想要拥有MAX_STIPEND
的用户名,则应转到窗口功能。例如,MS SQL Server中的Dense Rank。
with cte as
(select
f.name as "FACULTY_NAME",
s.name as "STUDENT_NAME",
stipend as "MAX_STIPEND",
DENSE_RANK() OVER
(PARTITION BY f.name, s.name ORDER BY i.stipend DESC) AS Rank
from
money m
inner join student s on m.student_id = s.id_student
inner join
faculty f on f.id_faculty = s.faculty_id
)
select "FACULTY_NAME", "STUDENT_NAME"
from cte
where rank = 1
并非所有的sql品牌都具有窗口功能。这里的dense_rank on MySQL以及dense_Rank for Oracle
的链接