SQL結合で表を結合する際に問題があります。私はテーブルCのNationalAvgとテーブルBの列SchoolAvgの両方を組み合わせたいと思いますが、私のクエリはどれも正しいわけではありません。私はSchoolAvgSQL結合結合
select Round(Cast(AVG(me.members_exams_score)as float), 2) as SchoolAvg, e.exams_description, s.schools_name
from members_exams as me
left join exams as e on e.exams_id = me.exams_id
join schools as s on s.schools_id = me.schools_id
where me.members_exams_score is not null
group by e.exams_description, s.schools_name
order by s.schools_name
結果
+--------------------+-------------------+--------------+
|SchoolAvg | exams_description | School |
+--------------------+-------------------+---------------
| .90 | Medical Asst.Exam | School A |
| .88 | Health Exam | School A |
| .65 | EKG Exam | School A |
| .76 | Phlebotomy | School A |
| .93 | Medical Asst.Exam | School B |
| .79 | Health Exam | School B |
| .82 | EKG Exam | School B |
| .76 | Phlebotomy | School B |
+--------------------+-------------------+--------------+
を得ることができます - 私はNationalAvg
Select Round(Cast(AVG(me.members_exams_score)as float), 2) as NationalAvg, e.exams_description
from members_exams as me
left join exams as e on e.exams_id = me.exams_id
join schools as s on s.schools_id = me.schools_id
where me.members_exams_score is not null
group by e.exams_description
結果
+--------------------+-------------------+
|NationalAvg | exams_description |
+--------------------+-------------------+
| .78 | Medical Asst.Exam |
| .90 | Health Exam |
| .79 | EKG Exam |
| .81 | Phlebotomy |
+--------------------+-------------------+
表Bを得ることができます -
表A
不正な結合表 - 両方のSchoolAvgが必要です。& NationalAvg。 NationalAvg列が表示されません。
select Round(Cast(AVG(me.members_exams_score)as float), 2) as SchoolAvg, e.exams_description, s.schools_name
from members_exams as me
left join exams as e on e.exams_id = me.exams_id
join schools as s on s.schools_id = me.schools_id
join
(select Round(Cast(AVG(me.members_exams_score)as float), 2) as NationalAvg
from members_exams as me
left join exams as e on e.exams_id = me.exams_id
join schools as s on s.schools_id = me.schools_id
group by e.exams_description) nAvg on e.exams_id = nAvg.NationalAvg
where me.members_exams_score is not null
group by s.schools_name, e.exams_description
結果 -
+------------------+--------------------+-------------------+--------------+
| School Avg |NationalAvg | exams_description | School |
+------------------+--------------------+-------------------+--------------|
| .90 | .78 | Medical Asst.Exam | School A |
| .88 | .90 | Health Exam | School A |
| .65 | .79 | EKG Exam | School A |
| .76 | .81 | Phlebotomy | School A |
| .93 | .78 | Medical Asst.Exam | School B |
| .79 | .90 | Health Exam | School B |
| .82 | .79 | EKG Exam | School B |
| .76 | .81 | Phlebotomy | School B |
+------------------+--------------------+-------------------+--------------+
おかげで、
素晴らしいです!乾杯&助けてくれてありがとう。 – Jay
@Jayを聞く素晴らしいニュース!その解決策を受け入れてくれてありがとう – Prob1em