已有一个名为'#FutureDatedExclude'在数据库中

时间:2016-05-25 13:51:22

标签: sql

SELECT FutureDatedEmployeeRecordsKey INTO #FutureDatedExclude
FROM dbo.vwRptDimEmployee_FutureDated FD1
WHERE EXISTS (SELECT 1 FROM dbo.vwRptDimEmployeeAll EE1 WITH (NOLOCK)
                    WHERE FD1.EmployeeID = EE1.EmployeeID AND FD1.EmployeeRecord = EE1.EmployeeRecord
                        AND FD1.JobEffectiveDate = EE1.JobEffectiveDT AND FD1.JobEffectiveDateSequence = EE1.JobEffectiveDateSequence
                        AND FD1.ActionCode = EE1.ActionCode AND FD1.ActionReasonCode = EE1.ActionReasonCode)

declare @JobStartDate date = '07/01/2014', @JobEndDate date = '06/30/2015'

SELECT DISTINCT
    E.LastName,
    E.SecondLastName,
    E.FirstName,
    E.MiddleName,
    E.PreferredName,
    E.PreferredFirstName,
    E.NameAC,
    E.LastNameAC,
    E.FirstNameAC,
    E.MiddleNameAC,
    E.GUI,
    E.EmployeeID,
    E.LPN,
    E.GPN,
    E.EmployeeRecord,
    E.JobEffectiveDT JobEffectiveDate,
    E.JobEffectiveDateSequence,
    E.ActionCode,
    E.Action,
    E.ActionDate,
    E.ActionReasonCode,
    AR.Description ActionReason,
    E.EmployeeStatusCode,
    E.EmployeeStatusDesc,
    CASE WHEN YEAR(E.LeaveEffectiveDT) > 2100 THEN NULL ELSE E.LeaveEffectiveDT END LeaveEffectiveDate,
    CASE WHEN YEAR(E.ExpectedReturnDate) > 2100 THEN NULL ELSE E.ExpectedReturnDate END ExpectedReturnDate,
    E.FullPartTime,
    E.ShiftCode FWACode,
    E.Shift FWAName,
    E.TeleWork,
    E.StandardHoursFrequency,
    E.StandardHours,
    E.FTE,
    E.PaidFTE,
    E.OvertimeEligibility,
    E.EmployeeClassCode,
    E.EmployeeClass,
    E.RegularVersusTemporary RegularTemporary,
    E.EmployeeType,
    E.PersonnelStatusDesc,
    E.PersonOrganizationRelationshipCode,
    P.PersonOfInterest,
    P.PersonOfInterestDesc,
    E.PaygroupCode,
    E.EmployeeCategoryCode,
    E.EmployeeSubcategoryCode,
    P.EmploymentCategory,
    E.NonEmployeeNonWorkTypeCD NonEmployeeNonWorkTypeCode,
    P.NonEmployeeNonWorkTypeDesc,
    A.GlobalAssignmentProgramCD GlobalAssignmentProgramCode,
    A.GlobalAssignmentProgramDesc,
    CASE WHEN YEAR(E.GlobalAssignmentStartDT) > 2100 THEN NULL ELSE E.GlobalAssignmentStartDT END GlobalAssignmentStartDate,
    CASE WHEN YEAR(E.GlobalAssignmentEndDT) > 2100 THEN NULL ELSE E.GlobalAssignmentEndDT END GlobalAssignmentEndDate,
    E.InPatExPatStatus,
    E.HomeCountry,
    E.HomeHostCountry HostCountry,
    CASE WHEN YEAR(E.EYStartDate) > 2100 THEN NULL ELSE E.EYStartDate END EYStartDate,
    CASE WHEN YEAR(E.LastRehireDate) > 2100 THEN NULL ELSE E.LastRehireDate END LastRehireDate,
    CASE WHEN YEAR(E.SeniorityDate) > 2100 THEN NULL ELSE E.SeniorityDate END SeniorityDate,
    CASE WHEN YEAR(E.EmployeeEffectiveDate) > 2100 THEN NULL ELSE E.EmployeeEffectiveDate END CurrentEmploymentDate,
    CASE WHEN YEAR(E.PartnerAdmissionDate) > 2100 THEN NULL ELSE E.PartnerAdmissionDate END PartnerAdmissionDate,
    R.RankCDName RankCodeName,
    E.Rank,
    R.RankDesc,
    E.BusinessTitle,--NEW
    R.RankGroup1,--NEW
    E.GFISRank,
    E.ExperienceLevel,
    E.GlobalGrade,
    E.JobCode,--NEW
    E.JobCDDesc JobCodeDesc,--NEW
    E.DepartmentCode,
    E.DepartmentName,
    E.CompanyCode,
    C.Description Company,
    C.DescrAc CompanyAC,
    E.ManagerialCountryCD ManagerialCountry,
    O.CodeBlock,
    O.BUCD BU,
    O.OUCD OU,
    O.MUCD MU,
    O.SMUCD SMU,
    O.BUName,
    O.OUName,
    O.MUName,
    O.SMUName,
    O.UserDefSLHierarchy1 ServiceLine,
    O.UserDefSLHierarchy2 SubSL1,
    O.UserDefSLHierarchy3 SubSL2,
    O.AlternateServiceLine,
    O.UserDefAreaHierarchy1 BULevel1,
    O.UserDefAreaHierarchy2 BULevel2,
    O.UserDefAreaHierarchy3 BULevel3,
    L.Location LocationCode,
    L.City LocationCity,
    L.State LocationStateProv,
    L.Country LocationCountry,
    L.UserDefinedHRGeo1 GeoLevel1,
    L.UserDefinedHRGeo2 GeoLevel2,
    L.UserDefinedHRGeo3 GeoLevel3,
    L.UserDefinedHRGeo4 GeoLevel4,
    L.UserDefinedHRGeo5 GeoLevel5,
    E.CounselorGUI,--NEW
    E.CounselorName,--NEW
    E.BillRate,
    E.Source,
     --**** confidential fields ****
    E.GenderCode,
    E.TermCD TermCode,
    E.TerminationReasonCode,
    E.CompensationCurrency,
    E.CompensationRate,
    E.CompensationFrequency,
    E.MonthlyCompensationRate,
    E.AnnualCompensationRate,
    CASE WHEN YEAR(P.SalaryEffectiveDT) > 2100 THEN NULL ELSE P.SalaryEffectiveDT END SalaryEffectiveDate,
    E.SalaryAdminPlanCode,
    E.SalaryAdminPlan,
    E.SalaryGrade,
    CASE WHEN YEAR(E.SalaryGradeEntryDate) > 2100 THEN NULL ELSE E.SalaryGradeEntryDate END SalaryGradeEntryDate,
    NULL JobKEY,
    NULL RowOrder

FROM dbo.vwRptFactEmployee F WITH (NOLOCK)  
    INNER JOIN dbo.vwRptDimEmployee E WITH (NOLOCK) ON (F.DimEmployeeKey = E.DimEmployeeKey) 
    INNER JOIN dbo.vwRptDimRank R WITH (NOLOCK) ON (F.DimRankKey = R.DimRankKey)
    INNER JOIN dbo.vwRptDimOrganization O WITH (NOLOCK) ON (F.DimOrganizationKey = O.DimOrganizationKey)
    INNER JOIN dbo.vwRptDimLocation L WITH (NOLOCK) ON (F.DimLocationKey = L.DimLocationKey)
    INNER JOIN dbo.vwRptDimAssignment A WITH (NOLOCK) ON (F.DimAssignmentKey = A.DimAssignmentKey)
    --INNER JOIN dbo.vwRptDimDate D WITH (NOLOCK) ON (F.TransEffectiveDateKey = D.DimDateKey)
    INNER JOIN dbo.vwRptDimEmployeeV2 P WITH (NOLOCK) ON (F.DimEmployeeKey = P.DimEmployeeKey)
    LEFT OUTER JOIN (SELECT ActionCode, ActionReasonCode, Description, row_number() over (partition by ActionCode, ActionReasonCode order by EffectiveDate DESC) as RowOrder
                    FROM PISupport.vwRptSetfActionReason WITH (NOLOCK)) AR
        ON (AR.ActionCode = E.ActionCode AND AR.ActionReasonCode = E.ActionReasonCode AND AR.RowOrder = 1)
    LEFT OUTER JOIN (SELECT DISTINCT C1.*, ROW_NUMBER() OVER (PARTITION BY CompanyCode ORDER BY EffectiveDate DESC) as RowOrder
                     FROM PISupport.vwRptSetfCompany C1 WITH (NOLOCK)) C
        ON (C.CompanyCode = E.CompanyCode AND C.RowOrder = 1)

WHERE (E.JobEffectiveDT BETWEEN @JobStartDate AND @JobEndDate)
--  AND (E.ActionCode in ('ADD','DTA','HIR','POI','REH','PER','TER'))
    --AND (O.BUCD+O.OUCD+O.MUCD+O.SMUCD LIKE '%'+@CodeBlock+'%' OR @CodeBlock IS NULL)
    --AND (E.GPN = @GPN OR @GPN IS NULL)
    --AND (E.GUI = @GUI OR @GUI IS NULL)
    --AND (L.UserDefinedHRGeo1 in (@GeoArea) )
    AND (L.UserDefinedHRGeo2 in ('UK and Ireland'))
    --AND (L.UserDefinedHRGeo3 in (@Country) )
    --AND (O.UserDefAreaHierarchy1 in (@Area) )
    --AND (O.UserDefAreaHierarchy2 in (@Region) )
    --AND (O.UserDefSLHierarchy1 in (@ServiceLine) )
    --AND (O.UserDefSLHierarchy2 in (@SubServiceLine) )
    --AND (R.RankCD in (@RankCode) )


UNION


SELECT DISTINCT * FROM (
SELECT DISTINCT
    ISNULL(E.LastName,N.LastName) LastName,
    ISNULL(E.SecondLastName,N.SecondLastName) SecondLastName,
    ISNULL(E.FirstName,N.FirstName) FirstName,
    ISNULL(E.MiddleName,N.MiddleName) MiddleName,
    E.PreferredName,
    ISNULL(E.PreferredFirstName,N.PreferredFirstName) PreferredFirstName,
    ISNULL(E.NameAC,N.NameAlternateCharacter) NameAC,
    E.LastNameAC,
    E.FirstNameAC,
    E.MiddleNameAC,
    E.GUI,
    FD.EmployeeID,
    FD.LPN,
    FD.GPN,
    FD.EmployeeRecord,
    FD.JobEffectiveDate,
    FD.JobEffectiveDateSequence,
    FD.ActionCode,
    FD.Action,
    FD.ActionDate,
    FD.ActionReasonCode,
    FD.ActionReason,
    FD.EmployeeStatusCode,
    FD.EmployeeStatus,
    NULL LeaveEffectiveDate,
    CASE WHEN YEAR(FD.ExpectedReturnDate) > 2100 THEN NULL ELSE FD.ExpectedReturnDate END ExpectedReturnDate,
    FD.FullPartTime,
    FD.ShiftCode FWACode,
    FD.Shift FWAName,
    NULL Telework,
    FD.StandardHoursFrequency,
    NULL StandardHours,
    FD.FTE,
    FD.PaidFTE,
    NULL OvertimeEligibility,
    FD.EmployeeClassCode,
    FD.EmployeeClass,
    FD.RegularVersusTemporary RegularTemporary,
    FD.EmployeeType,
    NULL PersonnelStatusDesc,
    FD.PersonOrganizationRelationshipCode,
    NULL PersonOfInterest,
    NULL PersonOfInterestDesc,
    FD.PaygroupCode,
    FD.EmployeeCategoryCode,
    FD.EmployeeSubcategoryCode,
    NULL EmploymentCategory,
    NULL NonEmployeeNonWorkTypeCode,
    NULL NonEmployeeNonWorkTypeDesc,
    NULL GlobalAssignmentProgramCode,
    NULL GlobalAssignmentProgramDesc,
    NULL GlobalAssignmentStartDate,
    NULL GlobalAssignmentEndDate,
    NULL InPatExPatStatus,
    NULL HomeCountry,
    NULL HostCountry,
    NULL EYStartDate,
    NULL LastRehireDate,
    NULL SeniorityDate,
    NULL CurrentEmploymentDate,
    NULL PartnerAdmissionDate,
    R.RankCDName RankCodeName,
    FD.Rank,
    R.RankDesc,
    FD.BusinessTitle,--NEW
    R.RankGroup1,--NEW
    FD.GFISRank,
    NULL ExperienceLevel,
    NULL GlobalGrade,
    FD.JobCode,--NEW
    NULL JobCodeDesc,--NEW
    FD.DepartmentCode,
    NULL DepartmentName,
    FD.CompanyCode,
    C.Description Company,
    C.DescrAc CompanyAC,
    NULL ManagerialCountry,
    O.CodeBlock,
    O.BUCD BU,
    O.OUCD OU,
    O.MUCD MU,
    O.SMUCD SMU,
    O.BUName,
    O.OUName,
    O.MUName,
    O.SMUName,
    O.UserDefSLHierarchy1 ServiceLine,
    O.UserDefSLHierarchy2 SubSL1,
    O.UserDefSLHierarchy3 SubSL2,
    O.AlternateServiceLine,
    O.UserDefAreaHierarchy1 BULevel1,
    O.UserDefAreaHierarchy2 BULevel2,
    O.UserDefAreaHierarchy3 BULevel3,
    L.Location LocationCode,
    L.City LocationCity,
    L.State LocationStateProv,
    L.Country LocationCountry,
    L.UserDefinedHRGeo1 GeoLevel1,
    L.UserDefinedHRGeo2 GeoLevel2,
    L.UserDefinedHRGeo3 GeoLevel3,
    L.UserDefinedHRGeo4 GeoLevel4,
    L.UserDefinedHRGeo5 GeoLevel5,
    NULL CounselorGUI,--NEW
    NULL CounselorName,--NEW
    NULL BillRate,
    FD.Source,
     --**** confidential fields ****
    NULL GenderCode,
    NULL TermCode,
    FD.TerminationReasonCode,
    FD.CompensationCurrency,
    FD.CompensationRate,
    FD.CompensationFrequency,
    FD.MonthlyCompensationRate,
    FD.AnnualCompensationRate,
    NULL SalaryEffectiveDate,
    FD.SalaryAdminPlanCode,
    FD.SalaryAdminPlan,
    FD.SalaryGrade,
    CASE WHEN YEAR(FD.SalaryGradeEntryDate) > 2100 THEN NULL ELSE FD.SalaryGradeEntryDate END SalaryGradeEntryDate,
    FD.Job_KEY,
    row_number() over (partition by FD.EmployeeID, FD.EmployeeRecord, FD.JobEffectiveDate,
        FD.JobEffectiveDateSequence, FD.ActionCode, FD.ActionReasonCode order by FD.Job_KEY DESC) as RowOrder

FROM dbo.vwRptDimEmployee_FutureDated FD
    INNER JOIN dbo.vwRptDimOrganization O WITH (NOLOCK) ON (FD.DimOrganizationKey = O.DimOrganizationKey)
    INNER JOIN dbo.vwRptDimLocation L WITH (NOLOCK) ON (FD.DimLocationKey = L.DimLocationKey)
    LEFT OUTER JOIN dbo.vwRptDimRank R WITH (NOLOCK) ON (FD.Rank = R.RankCD)
    LEFT OUTER JOIN dbo.vwRptDimEmployeeAll E WITH (NOLOCK) ON (FD.GPN = E.GPN AND FD.GPN <> '' AND E.RowIsCurrent = 'Y')
    LEFT OUTER JOIN (SELECT *, ROW_NUMBER() OVER (PARTITION BY EmployeeID, NameType ORDER BY EffectiveDate DESC) AS RowOrder
                    FROM PISupport.vwRptPersonACNames WITH (NOLOCK)
        ) N ON (N.EmployeeID = FD.EmployeeID AND N.NameType = 'PRI' AND N.CountryNameFormat = FD.SetIDLaborAgreement AND N.RowOrder = 1)
    LEFT OUTER JOIN (SELECT DISTINCT C1.*, ROW_NUMBER() OVER (PARTITION BY CompanyCode ORDER BY EffectiveDate DESC) as RowOrder
                     FROM PISupport.vwRptSetfCompany C1 WITH (NOLOCK)) C
        ON (C.CompanyCode = FD.CompanyCode AND C.RowOrder = 1)

WHERE
    FD.JobEffectiveDate BETWEEN @JobStartDate AND @JobEndDate
    AND FD.EDWIsCurrentRecord = 1
    AND FD.EmployeeID IS NOT NULL
    --AND (E.ActionCode in ('ADD','DTA','HIR','POI','REH','PER','TER'))
    --AND (O.BUCD+O.OUCD+O.MUCD+O.SMUCD LIKE '%'+@CodeBlock+'%' OR @CodeBlock IS NULL)
    --AND (FD.GPN = @GPN OR @GPN IS NULL)
    --AND (L.UserDefinedHRGeo1 in (@GeoArea) )
    AND (L.UserDefinedHRGeo2 in ('UK and Ireland'))
    --AND (L.UserDefinedHRGeo3 in (@Country) )
    --AND (O.UserDefAreaHierarchy1 in (@Area) )
    --AND (O.UserDefAreaHierarchy2 in (@Region) )
    --AND (O.UserDefSLHierarchy1 in (@ServiceLine) )
    --AND (O.UserDefSLHierarchy2 in (@SubServiceLine) )
    --AND (FD.Rank in (@RankCode) )
    AND FD.FutureDatedEmployeeRecordsKey NOT IN (SELECT FutureDatedEmployeeRecordsKey FROM #FutureDatedExclude)
) X
WHERE RowOrder = 1

DROP TABLE #FutureDatedExclude

1 个答案:

答案 0 :(得分:1)

您最有可能在代码底部添加DROP TABLE之前运行此代码。因此,表已创建但尚未删除。而不是在最后删除它,或者除了在你想要的时候删除它,把它放在脚本的最顶层:

IF OBJECT_ID('tempdb..#FutureDatedExclude') IS NOT NULL DROP TABLE #FutureDatedExclude