有人可以帮助我理解这个SQL查询

时间:2015-10-29 21:29:35

标签: sql

INSERT INTO Tbl_Basicinfo(Employer_Id,Lname,Fname,UserName,Userpass,Location_Id,cc2,CC3,cc4,cc5,Dohire,Job_Status,ssn,Import_Emp_No,[Benefit_Terminate], [Show_Age], [Change_IP], [IsNewHired])
SELECT M.Employer_Id,
            M.LASTNAME,
            M.FIRSTNAME,
            UPPER(LEFT(ISNULL(M.FIRSTNAME,'A'),1)+''+LEFT(ISNULL(M.LASTNAME,'C'),1))+CONVERT(VARCHAR,MaxImportEENumber+ROWNUM) [UserName],
            RIGHT(NEWID(),10) [UserPass],
            ISNULL(M.Location_Id,0)[Location_Id],
            M.Level2code,
            M.Level3code,
            M.LEVEL4Code,
            M.LEVEL5Code,
            M.DATEOFHIRE,
            1 [JobStatus],
            SSN,
            **M.MaxImportEENumber+ROWNUM [ImportEmpNo]**,
            0 [Benefit_Terminate], 
            0[Show_Age],
            M.ChangeIP,
            1 [IsNewHired] 
            from(
            ***SELECT ROW_NUMBER() OVER(ORDER BY [FIRSTNAME]) AS ROWNUM,**** FROM
            (
            ***SELECT DISTINCT 1117 [Employer_Id],PR.LASTNAME,PR.FIRSTNAME,(SELECT  MAX(CAST(ISNULL(Employee_Id,0) as BIGINT)) from Tbl_Basicinfo) [MaxImportEENumber]***,
            (SELECT TOP 1 Location_Id from Tbl_Location where Location_Code=PR.LEVEL1CODE and Employer_Id=1117 and Location like '%'+PR.LEVEL1DESCRIPTION+'%')  [Location_Id],
            (SELECT TOp 1 LEVEL2Code from Tbl_PPACA_Import where ssn=PR.SSN and EmployerId=PR.EmployerId) LEVEL2Code,
            (SELECT TOp 1 LEVEL3Code from Tbl_PPACA_Import where ssn=PR.SSN and EmployerId=PR.EmployerId) LEVEL3Code,
            (SELECT TOp 1 LEVEL4Code from Tbl_PPACA_Import where ssn=PR.SSN and EmployerId=PR.EmployerId) LEVEL4Code,
            (SELECT TOp 1 LEVEL5Code from Tbl_PPACA_Import where ssn=PR.SSN and EmployerId=PR.EmployerId) LEVEL5Code,
            (***SELECT MIN(DATEOFHIRE)*** from Tbl_PPACA_Import where ssn=PR.SSN and EmployerId=PR.EmployerId) DATEOFHIRE,1 Job_Status,PR.SSN,PR.ChangeIP

            from Tbl_PPACA_Import PR
            LEFT OUTER JOIN Tbl_Basicinfo L ON L.ssn=PR.SSN and L.Employer_Id=PR.EmployerId
            WHERE PR.EmployerId=1117 and L.SSN is null and ISNULL(PR.SSN,'') not like '' AND ISNUMERIC(PR.SSN)=1
            and ISNULL(PR.PPE,DATEOFHIRE) =***(SELECT MAX(ISNULL(PPE,DATEOFHIRE)) FROM Tbl_PPACA_Import I WHERE I.SSN=PR.SSN AND I.EmployerId=PR.EmployerId )*** 

                    ) V
            ) M;

注意:这个查询对我来说很完美,但有些人可以帮我排队,我在上面的sql查询中突出显示为粗体和斜体。这些线的目的是什么......

1 个答案:

答案 0 :(得分:2)

看起来这是尝试创建某种类型的唯一标识符。我的猜测是MaxImportEENumber获得最大的现有员工编号。然后,在此特定选择中添加行的行号后,[ImportEmpNo]将是从返回的第一行开始计算的最大现有员工编号开始的序号。