需要SQL查询的帮助

时间:2013-07-29 13:53:57

标签: sql sql-server-2008-r2

我有3个表,我正在尝试创建一个查询:
表1(iuieEmployee) - >职位编号
表2(jbEmployeeH1BInfo) - >位置编号,LCA编号,开始日期
表3(jbEmployeeLCA) - > LCA号码 表4(jbInternationsl) - >主要人口统计表

我的查询在每个表中只有1条记录的情况下工作正常,但表2和3可以有多条记录。我希望它找到最近开始日期的记录并验证第3个表中是否存在匹配的LCA编号,并在第一个表中显示匹配的位置编号,并向我显示任何不是这种情况的记录。我怎么能做到这一点?我目前有:

SELECT DISTINCT jbInternational.idnumber, jbInternational.lastname, jbInternational.firstname, jbInternational.midname, 
            jbInternational.campus, jbInternational.universityid, jbInternational.sevisid, jbInternational.citizenship,
            jbInternational.immigrationstatus, jbEmployeeH1BInfo.lcaNumber AS lcaNumber1, jbEmployeeLCA.lcaNumber AS lcaNumber2

FROM (select jbEmployeeH1BInfo.idnumber, MAX(jbEmployeeH1BInfo.approvalStartDate) AS MaxDateStamp FROM [internationalservices].[dbo].jbEmployeeH1BInfo GROUP BY idnumber ) my
            INNER JOIN [internationalservices].[dbo].jbEmployeeH1BInfo WITH (nolock) ON my.idnumber=jbEmployeeH1BInfo.idnumber AND my.MaxDateStamp=jbEmployeeH1BInfo.approvalStartDate
            INNER JOIN [internationalservices].[dbo].jbInternational WITH (nolock) ON jbInternational.idnumber=jbEmployeeH1BInfo.idnumber 
            inner join [internationalservices].[dbo].jbEmployeeLCA ON jbInternational.idnumber = jbEmployeeLCA.idnumber

WHERE jbInternational.idnumber not in(

SELECT DISTINCT jbInternational.idnumber

FROM (select distinct jbEmployeeH1BInfo.idnumber, MAX(jbEmployeeH1BInfo.approvalStartDate) AS MaxDateStamp
            FROM [internationalservices].[dbo].jbEmployeeH1BInfo GROUP BY idnumber ) my
            INNER JOIN [internationalservices].[dbo].jbEmployeeH1BInfo WITH (nolock) ON my.idnumber=jbEmployeeH1BInfo.idnumber AND my.MaxDateStamp=jbEmployeeH1BInfo.approvalStartDate
            INNER JOIN [internationalservices].[dbo].jbInternational WITH (nolock) ON jbInternational.idnumber=jbEmployeeH1BInfo.idnumber 
            inner join [internationalservices].[dbo].jbEmployeeLCA ON jbInternational.idnumber = jbEmployeeLCA.idnumber
            AND jbEmployeeH1BInfo.lcaNumber = jbEmployeeLCA.lcaNumber)

表格式:

create table iuieEmployee(idnumber int, POS_NBR varchar(8));
insert into iuieEmployee values(123456, '470V13');
insert into iuieEmployee values(123457, '98X000');
insert into iuieEmployee values(123458, '98X000');
insert into iuieEmployee values(123455, '98X000');

create table jbEmployeeH1BInfo (idnumber int, approvalStartDate smalldatetime, lcaNumber varchar(20), positionNumber varchar(200));
insert into jbEmployeeH1BInfo values (123456, 07/01/2012, '1-200-3000', '98X000');
insert into jbEmployeeH1BInfo values (123456, 07/30/2013, '1-200-4000', '470V13');
insert into jbEmployeeH1BInfo values (123457, 07/01/2012, '1-200-5000', '98X000');
insert into jbEmployeeH1BInfo values (123458, 07/01/2012, '1-200-6000', '98X000');
insert into jbEmployeeH1BInfo values (123455, 07/30/2014, '1-200-7000', '98X000');
insert into jbEmployeeH1BInfo values (123455, 07/01/2012, '1-200-8000', '470V13');

create table jbEmployeeLCA (idnumber int, lcaNumber varchar(20));  
insert into jbEmployeeLCA values (123456, 1-200-3000);
insert into jbEmployeeLCA values (123456, 1-200-4111);
insert into jbEmployeeLCA values (123457, 1-200-5000);
insert into jbEmployeeLCA values (123458, 1-200-6000);
insert into jbEmployeeLCA values (123455, 1-200-7000);
insert into jbEmployeeLCA values (123455, 1-200-8000);

create table jbInternational(idnumber int);
insert into jbInternational values(123456);
insert into jbInternational values(123457);
insert into jbInternational values(123458);
insert into jbInternational values(123455);

只应返回1行:
123456,07 / 30/2013,'1-200-4000'

而是返回两行:
123456,07 / 30/2013,'1-200-4000(不匹配1-200-4111)
123456,07 / 30/2013,'1-200-4000(不匹配1-200-3000)
它不应该返回第二行,因为具有-3000 lca数字的位置编号没有最新的日期。

1 个答案:

答案 0 :(得分:0)

您的解释很难理解。我想如果你能解释得那么好,那么你可以自己编写查询。这就是我认为你的意思:

员工包含主要记录。

你想要找到所有的idnumber

  • idnumber在国际
  • 具有最新ApproStartDate的H1BInfo记录没有与LCA记录匹配的LCA编号

首先要做的是简化H1BInfo表。我们只查找具有最新ApproStartDate的行。我们可以通过idnumber分区并通过approvalStartDate进行排序:

with rankedH1BInfo as (
    SELECT *, 
        ROW_NUMBER() OVER (PARTITION BY jbEmployeeH1BInfo.idnumber
            ORDER BY jbEmployeeH1BInfo.approvalStartDate desc) as r 
    FROM [internationalservices].[dbo].jbEmployeeH1BInfo
)

让我们只获取每个分区的第一行:

, MostRecentH1BInfo as (
    SELECT * FROM rankedH1BInfo
    WHERE r = 1
)

现在我们可以进行联接以找到所有好的:

, goodIDs as (
    SELECT i.idnumber
    FROM [internationalservices].[dbo].jbInternational i WITH (NOLOCK)
    JOIN [internationalservices].[dbo].jbEmployeeLCA l WITH (NOLOCK) on l.idnumber = i.idnumber
    JOIN MostRecentH1BInfo h WITH (NOLOCK) on h.idnumber = i.idnumber
    JOIN iuieEmployee e WITH (NOLOCK) on e.positionNumber = h.positionNumber
    WHERE h.lcaNumber = l.lcaNumber
)

把它们放在一起,得到那些错误的地方:

with rankedH1BInfo as (
    SELECT *, 
        ROW_NUMBER() OVER (PARTITION BY jbEmployeeH1BInfo.idnumber
            ORDER BY jbEmployeeH1BInfo.approvalStartDate desc) as r 
    FROM [internationalservices].[dbo].jbEmployeeH1BInfo
), MostRecentH1BInfo as (
    SELECT * FROM rankedH1BInfo
    WHERE r = 1
), goodIDs as (
    SELECT i.idnumber
    FROM [internationalservices].[dbo].jbInternational i WITH (NOLOCK)
    JOIN [internationalservices].[dbo].jbEmployeeLCA l WITH (NOLOCK) on l.idnumber = i.idnumber
    JOIN MostRecentH1BInfo h WITH (NOLOCK) on h.idnumber = i.idnumber
    JOIN iuieEmployee e WITH (NOLOCK) on e.positionNumber = h.positionNumber
    WHERE h.lcaNumber = l.lcaNumber
)
SELECT DISTINCT jbInternational.idnumber, jbInternational.lastname, jbInternational.firstname, jbInternational.midname, 
        jbInternational.campus, jbInternational.universityid, jbInternational.sevisid, jbInternational.citizenship,
        jbInternational.immigrationstatus, jbEmployeeH1BInfo.lcaNumber AS lcaNumber1, jbEmployeeLCA.lcaNumber AS lcaNumber2
FROM (select jbEmployeeH1BInfo.idnumber, MAX(jbEmployeeH1BInfo.approvalStartDate) AS MaxDateStamp FROM [internationalservices].[dbo].jbEmployeeH1BInfo GROUP BY idnumber ) my
            INNER JOIN [internationalservices].[dbo].jbEmployeeH1BInfo WITH (nolock) ON my.idnumber=jbEmployeeH1BInfo.idnumber AND my.MaxDateStamp=jbEmployeeH1BInfo.approvalStartDate
            INNER JOIN [internationalservices].[dbo].jbInternational WITH (nolock) ON jbInternational.idnumber=jbEmployeeH1BInfo.idnumber 
            inner join [internationalservices].[dbo].jbEmployeeLCA ON jbInternational.idnumber = jbEmployeeLCA.idnumber
WHERE jbInternational.idnumber not in (select idnumber from goodIDs)