SQL连接 - 加入17个表 - 不显示数据的列

时间:2017-03-09 14:39:10

标签: sql join left-join inner-join union-all

DataDiagram

上面是我想查询几个字段的数据库的部分数据图。 “tPlanningPeriodText”中的数据& “tSubPlanningPeriodText”表返回NULL。我该如何解决?这个表的链接(JOIN)是不是“到达”了吗?

以下是查询: 附:这不是我的数据库我只是想从中查询数据

SELECT  T2.GlobalEmpCode "Global Employee Code", T2.EmpCode "Employee Code", T3.LastName "Last Name", 
    T3.FirstName "First Name", T3.LastName + ',' + T3.FirstName "Full Name", T1.CompletePercent "Completition (%)", 
    T5.Email, T9.Title "Program", T7.Title "Goal", T6.TargetUnitDesc "Unit", T11.Title "Position Title", T15.Code "Planning Period", 
    T17.Code "Sub-Planning Period"
FROM tEmployeePlan AS T1
    LEFT JOIN tEmployee AS T2 ON T1.PID=T2.PID
    LEFT JOIN tPerson AS T3 ON T2.PID=T3.PID
    LEFT JOIN tIdentity AS T4 ON T3.PID=T4.PID
    LEFT JOIN tUser AS T5 ON T4.UserID=T5.UserID
    LEFT JOIN tGoal AS T6 ON T1.PID = T6.ProposalPID
    LEFT JOIN tGoalText AS T7 ON T6.GoalID = T7.GoalTID
    LEFT JOIN tProgram AS T8 ON T1.PgmID = T8.PgmID
    LEFT JOIN tProgramText AS T9 ON T8.PgmID = T9.PgmID
    LEFT JOIN tEmployeePlanPosition AS T10 ON T1.EmpPlanID = T10.EmpPlanID
    LEFT JOIN tEmployeePlanPositionText AS T11 ON T10.EmpPlanID = T11.EmpPlanID

    LEFT JOIN tGoalCatalogItem AS T12 ON T6.GoalID = T12.GoalID
    LEFT JOIN tGoalCatalog AS T13 ON T12.GoalCatalogID = T13.GoalCatalogID
    LEFT JOIN tPlanningPeriod AS T14 ON T13.PlanPerID = T14.PlanPerID
    LEFT JOIN tPlanningPeriodText AS T15 ON T14.PlanPerID = T15.PlanPerID
    LEFT JOIN tSubPlanningPeriod AS T16 ON T14.PlanPerID = T16.PlanPerID
    LEFT JOIN tSubPlanningPeriodText AS T17 ON T16.SubPlanPerID = T17.SubPlanPerID

ORDER BY GlobalEmpCode;

1 个答案:

答案 0 :(得分:0)

SELECT DISTINCT T2.GlobalEmpCode "Global Employee Code", T2.EmpCode "Employee Code", T3.LastName "Last Name", 
    T3.FirstName "First Name", T3.LastName + ',' + T3.FirstName "Full Name",

    --CASE statement picking between 3 business unit levels i.e. division, section, department
    "Division" = CASE WHEN T35.UnitTreeTypeID = -1 AND T35.Level = 1 THEN T30.Code END, 
    "Section" = CASE WHEN T35.UnitTreeTypeID = -1 AND T35.Level = 2 THEN T30.Code END, 
    "Department" = CASE WHEN T35.UnitTreeTypeID = -1 AND T35.Level = 3 THEN T30.Code END, 

    T1.CompletePercent "Completition (%)",

    --CASE statement to formulate plan status as per completion percentage
    "Status" = CASE WHEN T1.CompletePercent = 0 THEN 'Not Started'
                    WHEN T1.CompletePercent = 20 THEN 'Draft'
                    WHEN T1.CompletePercent = 40 THEN 'Agreement'
                    WHEN T1.CompletePercent = 60 THEN 'Agreed'
                    WHEN T1.CompletePercent = 80 THEN 'Evaluation'
                    WHEN T1.CompletePercent = 100 THEN 'Completed'
                END,

    T5.Email, T9.Title "Program", T7.Title "Goal",T24.GlobalCode "Goal Category", T6.TargetUnitDesc "Unit", T11.Title "Position Title", T13.Code "Planning Period",
    T23.Code "Sub-Planning Period", T15.LastName + ',' + T15.FirstName "Plan Owner"
FROM tEmployeePlan AS T1
    LEFT JOIN tEmployee AS T2 ON T1.PID=T2.PID
    LEFT JOIN tPerson AS T3 ON T2.PID=T3.PID
    LEFT JOIN tIdentity AS T4 ON T3.PID=T4.PID
    LEFT JOIN tUser AS T5 ON T4.UserID=T5.UserID
    LEFT JOIN tGoal AS T6 ON T1.PID = T6.ProposalPID
    LEFT JOIN tGoalText AS T7 ON T6.GoalID = T7.GoalTID
    LEFT JOIN tGoalCategory AS T24 ON T6.GoalCatID = T24.GoalCatID
    LEFT JOIN tProgram AS T8 ON T1.PgmID = T8.PgmID
    LEFT JOIN tProgramText AS T9 ON T8.PgmID = T9.PgmID 
    LEFT JOIN tEmployeePlanPosition AS T10 ON T1.EmpPlanID = T10.EmpPlanID
    LEFT JOIN tEmployeePlanPositionText AS T11 ON T10.EmpPlanID = T11.EmpPlanID
    LEFT JOIN tPlanningPeriod AS T12 ON T8.PlanPerID = T12.PlanPerID
    LEFT JOIN tPlanningPeriodText AS T13 ON T12.PlanPerID = T13.PlanPerID
    LEFT JOIN tSubPlanningPeriodText AS T23 ON T12.PlanPerID = T23.SubPlanPerID
    LEFT JOIN tEmployeePlanOwner AS T14 ON T1.EmpPlanID = T14.EmpPlanID
        --validation
        AND T14.PlanOwnerTypeID = -1 --person direct (supervisor)
        AND T14.OwnerPID <> -1 --person not direct
    LEFT JOIN tPerson AS T15 ON T14.OwnerPID = T15.PID
    LEFT JOIN tSlot AS T25 ON T2.PID = T25.PID
        AND T25.IsPrimaryPos = 1
        AND T25.PosID <> -1
    LEFT JOIN tPosition AS T26 ON T25.PosID = T26.PosID
        AND T26.PosID <> -1
    LEFT JOIN tPositionUnit AS T27 ON T26.PosID = T27.PosID
        AND T27.UnitTreeTypeID = -1
    LEFT JOIN tUnit AS T28 ON T27.UnitID = T28.UnitID
    LEFT JOIN tUnitType AS T35 ON T28.UnitTypeID = T35.UnitTypeID
    LEFT JOIN tUnitTypeText AS T29 ON T28.UnitTypeID = T29.UnitTypeID
    LEFT JOIN tUnitText AS T30 ON T28.UnitID = T30.UnitID
        AND T30.IsDefault = 1
ORDER BY T2.GlobalEmpCode;