2017-01-24 3 views
1

내 ETL 쿼리는 직원에 대한 정보를 반환하고 하위 쿼리에서 오는 두 개의 집계 된 열 (ActualCountExpectedCount)을가집니다.하위 쿼리에서 하위 쿼리의 날짜를 필터링하는 방법

내가 겪고있는 문제는 하위 쿼리가 남아있는 테이블이 집계 된 정보와 독립적으로 업데이트된다는 것입니다. 따라서 쿼리를 점진적으로 실행하면 쿼리에서 t1.ModifiedDate가 업데이트되지 않았고 카운트 레코드가 업데이트되었지만 새 카운트 레코드가 반환되지 않습니다.

증분로드에 대해 원하는 결과를 반환하는이 쿼리를 작성했지만 이제는 초기로드 중에 각 ID에 대해 여러 개의 결과가 반환되고 PK 제약 조건을 위반합니다. 이는 하위 필터링에서 추가 한 t2.ModifiedDate 열로 인해 필터링해야합니다.

t2.ModifiedDate를 가져와 중복을 생성하지 않고 기본 쿼리로 가져 오는 방법이 있습니까?

원하는 결과 (나는이 증가 테스트 한 결과 추가) :

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와 (SubDate, Month of Year) SubmissionDate의 각 조합에 대해 여러 행을 가질 수 있다고 가정합니다. 해당 값으로 그룹화하는 대신'MAX (t2.ModifiedDate) AS ModifiedDate'를 선택하여 문제를 수정할 수 있습니다. –

답변

1

대신에 하위 쿼리에서 최대 ModifiedDate를 운반 where 절에 사용하면 exists()을 대신 사용할 수 있습니다. Vergil 아마 더 좋을 것이다 웃음에 의해 제안

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'가 없으므로 ores 절의 ise 테이블에서이 테이블을 조인했습니다. 그러나 이제는 모든 연도의 행을 반환합니다. 변경 사항을 표시하기 위해 질문을 업데이트하겠습니다. –

+0

은'InspectionSubmissionEmployee'에서'EmployeeID'를 얻기 위해 join을 포함하도록'exists()'를 업데이트했습니다 - 그 달의 데이터가> x로 수정 된 경우 각 달에 대한 집계 데이터 만 필요하면 왜 그 데이터를 하위 쿼리'a'에서'where'? – SqlZim

+0

그게 내가 한 짓이긴하지만, 한 직원에 대한 모든 이전 연도가 반환됩니다. whos 정보가 예상 한 행과 반대로 업데이트되었습니다. –