2016-09-27 105 views
0

无法在SQL联接中合并表。我想将表A中的NationalAvg表A列和表B中的SchoolAvg列组合起来,但是我的查询都不正确。SQL联合加入

表A - 我在哪里可以得到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 - 我在哪里可以得到SchoolAvg

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。 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 

结果 - 被带回同一个表B.
预期的结果 -

+------------------+--------------------+-------------------+--------------+ 
| 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  | 
+------------------+--------------------+-------------------+--------------+ 

感谢,

回答

0

尝试增加你的逻辑来的CTE和加盟的方式。

With CTE1 as(
    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 
),CTE2 as (
    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 
) 
select A.exams_description, A.NationalAvg, B.schools_name, B.SchoolAvg 
from CTE A 
left Join CTE1 B ON A.exams_description = B.exams_description 
+0

它很好用!欢呼和感谢您的帮助。 – Jay

+0

听到@Jay的好消息!感谢您接受该解决方案 – Prob1em

0

通过查看您的输入输出示例,您希望加入列exams_description上的两个表。

由于您已经有了自己编写的两个子查询,因此您可以对两个子查询的结果进行连接。

with T1 as(
    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 
),T2 as (
    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 
) 
select T2.SchoolAvg, T1.NationalAvg, T2.exams_description, T1.exams_description 
from T1 Join T2 
where T1.examples_description = T2.examples_description;