SQL组合加入

时间:2016-09-27 20:35:32

标签: sql sql-server join

在SQL连接中组合表时遇到问题。我想在表C中将NationalAvg的表A列和表B列SchoolAvg结合起来,但我的查询都不正确。

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

错误的组合表 - 想要两个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

结果 - 带回与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     |
+------------------+--------------------+-------------------+--------------+

谢谢,

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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;