2017-01-24 13 views
1

ETLクエリでは従業員に関する情報が返され、サブクエリに由来する2つの集計カラム(ActualCountおよびExpectedCount)が返されます。サブクエリ以外のサブクエリで日付をフィルタリングする方法

問題は、サブクエリが残っているテーブルが集約された情報とは独立して更新されていたことでした。だから私はクエリを段階的に実行していたときに、クエリはt1.ModifiedDateが更新されておらず、カウントレコードが更新されていても新しいカウントレコードを返しません。

インクリメンタルロードの目的の結果を返すこのクエリを書きましたが、私の問題は最初のロード中に各IDに対して複数の結果が返され、PK制約に違反することです。これはサブクエリに追加したt2.ModifiedDate列のためです。サブクエリでフィルタリングする必要があります。

t2.ModifiedDateを取得し、重複を生成せずにメインのクエリに持ち出す方法はありますか?

望ましい結果(私はこの増分テストのための1つの結果を追加):

enter image description here

現在のクエリ:

SELECT t1.EmployeeGoalID , 
     t1.EmployeeID , 
     t1.EmployeeMonthlyGoal , 
     t1.TargetMonth , 
     t1.TargetYear , 
     Actuals.BranchID , 
     Actuals.ActualCount , 
     CASE WHEN Actuals.ActualCount IS NULL THEN 0 
      WHEN Actuals.ActualCount < t1.EmployeeMonthlyGoal 
      THEN Actuals.ActualCount 
      ELSE t1.EmployeeMonthlyGoal 
     END AS ExpectedCount , 
     t1.CreateDate , 
     t1.ModifiedDate , 
     t1.Deleted 
FROM dbo.EmployeeGoal t1 
     LEFT JOIN (SELECT COUNT(DISTINCT t2.InspectionSubmissionResultID) AS ActualCount , 
          t2.BranchID , 
          t3.EmployeeID , 
          MONTH(DATEADD(hh, -5, t2.SubmissionDate)) AS ActualMonth , 
          YEAR(DATEADD(hh, -5, t2.SubmissionDate)) AS ActualYear, 
          t2.ModifiedDate -- <<<<<This causes the problem 
        FROM InspectionSubmissionResult t2 
          INNER JOIN dbo.InspectionSubmissionEmployee t3 ON t3.InspectionSubmissionResultID = t2.InspectionSubmissionResultID 
        WHERE t3.InspectorType = 'INSP' 
        GROUP BY t2.BranchID , 
          t3.EmployeeID , 
          MONTH(DATEADD(hh, -5, t2.SubmissionDate)) , 
          YEAR(DATEADD(hh, -5, t2.SubmissionDate)) , 
          t2.ModifiedDate -- <<<<This causes the problem 
       ) AS Actuals ON Actuals.EmployeeID = t1.EmployeeID 
            AND t1.TargetMonth = Actuals.ActualMonth 
            AND t1.TargetYear = Actuals.ActualYear 
WHERE Actuals.ModifiedDate > '1/23/2017' OR t1.ModifiedDate > '1/23/2017' 
-- I need this Actuals.ModifiedDate 
+1

http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to-kick-using-table-aliases-like-abc-or-t1-t2- t3.aspx –

+2

あなたの実際の質問については、多くの助力を提供するためにはもう少し情報が必要です。ここから始めましょう。 http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/あなたのコメントは「これは問題を引き起こします」です。何が問題ですか? –

+2

私は 'InspectionSubmissionResult'テーブルがBranchIDと(月、年)SubmissionDateの各組み合わせに対して複数の行を持つことができると仮定しています。その値でグループ化するのではなく、 'MAX(t2.ModifiedDate)AS ModifiedDate'を選択することで問題を修正できます。 –

答えて

1

代わりにサブクエリからアップModifiedDateを運びますwhere句で使用する場合は、代わりにexists()を使用してください。バージルはおそらく良いだろう笑うによって提案

select 
     eg.EmployeeGoalID 
    , eg.EmployeeID 
    , eg.EmployeeMonthlyGoal 
    , eg.TargetMonth 
    , eg.TargetYear 
    , a.BranchID 
    , a.ActualCount 
    , ExpectedCount = case 
     when a.ActualCount is null then 0 
     when a.ActualCount < eg.EmployeeMonthlyGoal then a.ActualCount 
     else eg.EmployeeMonthlyGoal 
     end 
    , eg.CreateDate 
    , eg.ModifiedDate 
    , eg.Deleted 
from dbo.EmployeeGoal eg 
    left join (
    select 
     ActualCount = count(distinct isr.InspectionSubmissionResultID) 
     , isr.BranchID 
     , ise.EmployeeID 
     , ActualMonth = month(dateadd(hh, - 5, isr.SubmissionDate)) 
     , ActualYear = year(dateadd(hh, - 5, isr.SubmissionDate)) 
     --, isr.ModifiedDate -- <<<<<This causes the problem 
    from dbo.InspectionSubmissionResult isr 
    inner join dbo.InspectionSubmissionEmployee ise 
     on ise.InspectionSubmissionResultID = isr.InspectionSubmissionResultID 
    where ise.InspectorType = 'insp' 
    group by 
     isr.BranchID 
     , ise.EmployeeID 
     , month(dateadd(hh, - 5, isr.SubmissionDate)) 
     , year(dateadd(hh, - 5, isr.SubmissionDate)) 
     --, isr.ModifiedDate -- <<<<This causes the problem 
    ) as a 
     on a.EmployeeID = eg.EmployeeID 
     and eg.TargetMonth = a.ActualMonth 
     and eg.TargetYear = a.ActualYear 
    where eg.ModifiedDate > '1/23/2017' 
    -- or a.ModifiedDate > '1/23/2017' 
    or exists (
     select 1 
     from dbo.InspectionSubmissionResult isr 
      inner join dbo.InspectionSubmissionEmployee ise 
      on ise.InspectionSubmissionResultID 
       = isr.InspectionSubmissionResultID 
     where ise.EmployeeId = eg.EmployeeId 
      and isr.ModifiedDate > '1/23/2017' 
      and month(dateadd(hh, - 5, isr.SubmissionDate))=eg.TargetMonth 
      and year(dateadd(hh, - 5, isr.SubmissionDate))=eg.TargetYear 
     ) 

max(isr.ModifiedDate)方法:

はここにあなたの変更を伴うクエリ、およびいくつかの再フォーマットです。

select 
     eg.EmployeeGoalID 
    , eg.EmployeeID 
    , eg.EmployeeMonthlyGoal 
    , eg.TargetMonth 
    , eg.TargetYear 
    , a.BranchID 
    , a.ActualCount 
    , ExpectedCount = case 
     when a.ActualCount is null then 0 
     when a.ActualCount < eg.EmployeeMonthlyGoal then a.ActualCount 
     else eg.EmployeeMonthlyGoal 
     end 
    , eg.CreateDate 
    , eg.ModifiedDate 
    , eg.Deleted 
from dbo.EmployeeGoal eg 
    left join (
    select 
     ActualCount = count(distinct isr.InspectionSubmissionResultID) 
     , isr.BranchID 
     , ise.EmployeeID 
     , ActualMonth = month(dateadd(hh, - 5, isr.SubmissionDate)) 
     , ActualYear = year(dateadd(hh, - 5, isr.SubmissionDate)) 
     , ModifiedDate = max(isr.ModifiedDate) -- <<<<<This causes the problem 
    from dbo.InspectionSubmissionResult isr 
    inner join dbo.InspectionSubmissionEmployee ise 
     on ise.InspectionSubmissionResultID = isr.InspectionSubmissionResultID 
    where ise.InspectorType = 'insp' 
    group by 
     isr.BranchID 
     , ise.EmployeeID 
     , month(dateadd(hh, - 5, isr.SubmissionDate)) 
     , year(dateadd(hh, - 5, isr.SubmissionDate)) 
     --, isr.ModifiedDate -- <<<<This causes the problem 
    ) as a 
     on a.EmployeeID = eg.EmployeeID 
     and eg.TargetMonth = a.ActualMonth 
     and eg.TargetYear = a.ActualYear 
    where eg.ModifiedDate > '1/23/2017' 
    or a.ModifiedDate > '1/23/2017' 
+0

isrテーブルには 'EmployeeID'がありませんので、or節のiseテーブルに参加しましたが、現在はすべての年の行を返しています。変更を示すために質問を更新します。 –

+0

'InspectionSubmissionEmployee'から' EmployeeID'を取得するための結合を含める 'exists()'を更新しました - その月のデータが> xに変更された場合に毎月の集計データが必要な場合は、サブクエリ 'a'の' where'ですか? – SqlZim

+0

これも私がやったことですが、1人の従業員のためにすべての前の年が返されています。予想される1行に対して情報が更新されました –

関連する問題