2017-03-09 9 views
0
以上

DataDiagramSQLジョイン - 列がデータ

は私からいくつかのフィールドを照会したいデータベースの一部のデータ図である表示されない - 17個のテーブルに参加します。 "tPlanningPeriodText" & "tSubPlanningPeriodText"テーブルのデータがNULLを返しています。これをどうやって解決するのですか?このテーブルへのリンク(JOIN)はそれに「到達」していませんか?

以下はクエリです。 P.S.これは私がちょうど

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

LEFTは通常、右サイドテーブルのためにいくつかのNULLのを返しますJOINのそれからデータを照会しようとしている私のデータベースではありません。 – jarlh

+0

私はいくつかのNULLで大丈夫です、私はそれも期待して、問題はNULL値を含んでいないレコードがないとtable.columnは値を持っています – Ramollo

+2

データを見ることなく答えにくい。私はselect tEmployeeを試し、tGoalに参加し、tGoalCatalogItemに参加し、あなたが期待するものが得られるかどうかを見ます。その場合は、予期しないnullが表示されるまで、一度に1つずつ結合を追加します。 –

答えて

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; 
関連する問題