以下查询选择学生姓名及其特定行的最高分数。问题是无法使用左连接中的子查询。 我收到Teradata数据错误:“对象Date_View不存在”。如何解决这个问题?
Select name, max_score_today.max_score From Student_View
Left Join Date_view
ON Date_View.date=Student_View.date
Left Join (
Select MAX(score) as max_score FROM
Score_View
Where Date_View.start_date=Score_View.date
) max_score_today
ON max_score_today.name=Student_View.name
答案 0 :(得分:2)
将相关子查询向上移动到select语句中,例如:
select name,
(select max(score) from score_view where date_view.start_date) = score_view.date) as max_score
from student_view
left join
date_view
on date_view.date = student_view.date;
答案 1 :(得分:1)
要获得具有最高/最低值的行,最好使用RANK
或ROW_NUMBER
,如下所示:
Select *
From Student_View
Left Join Date_view
on Date_View.date=Student_View.date
Left Join Score_View
on Date_View.start_date=Score_View.date
and Score_View.name=Student_View.name
QUALIFY -- get the highest score for each student/date
ROW_NUMBER() -- maybe RANK
OVER (PARTITION BY Student_View.name, Score_View.date
ORDER BY Score_View.score DESC) = 1
我不确定这是否是正确的结果,您可能需要更改PARTITION表达式...