基于条件的SQL选择

时间:2018-11-18 20:45:34

标签: sql ms-access

在尝试在MS Access 2016中创建以下查询时,我尝试绕过这个问题,但似乎还不够。 我的目标是执行以下操作:“列出具有15年或15年以上工作经验的Tigers每位教练的姓名和工作经验总数。” 以下是3个不同的表格:团队,教练,工作经验。 造成我最大问题的是,计算教练所拥有的所有工作经验。

查询的预期结果应产生:

教练姓名| YRS经验总数

ADAMS | 25

image1
image2
image3

1 个答案:

答案 0 :(得分:0)

考虑:

查询1:SumExp

SELECT Sum(WorkExperience.WEXP_Yrs) AS SumOfWEXP_Yrs, WorkExperience.CoachNum
FROM WorkExperience
GROUP BY WorkExperience.CoachNum;

查询2:

SELECT WorkExperience.TeamNum, Coach.CoachName, SumExp.SumOfWEXP_Yrs
FROM 
    Coach INNER JOIN 
    (
        SumExp INNER JOIN 
        (
            Team INNER JOIN WorkExperience 
            ON Team.TeamNum = WorkExperience.TeamNum
        )
        ON SumExp.CoachNum = WorkExperience.CoachNum
    )
    ON Coach.CoachNum = SumExp.CoachNum
WHERE 
    WorkExperience.TeamNum = 24;

多合一:

SELECT WorkExperience.TeamNum, Coach.CoachName, SumExp.SumOfWEXP_Yrs
FROM 
    Coach INNER JOIN
    (
        (
            SELECT Sum(WorkExperience.WEXP_Yrs) AS SumOfWEXP_Yrs, WorkExperience.CoachNum
            FROM WorkExperience
            GROUP BY WorkExperience.CoachNum
        ) AS SumExp 
        INNER JOIN 
        (
            Team INNER JOIN WorkExperience 
            ON Team.TeamNum = WorkExperience.TeamNum
        ) 
        ON SumExp.CoachNum = WorkExperience.CoachNum
    )
    ON Coach.CoachNum = SumExp.CoachNum
WHERE 
    WorkExperience.TeamNum = 24;