0

我有 3 个表,我试图从中创建查询:
表 1 (iuieEmployee) -> 职位编号
表 2 (jbEmployeeH1BInfo) -> 职位编号、LCA 编号、开始日期
表 3 (jbEmployeeLCA) -> LCA 编号
表 4 (jbInternationsl) -> 主要人口统计表

我有一个工作正常的查询,其中每个表中只有 1 条记录,但表 2 和 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 (not matching 1-200-3000)
它不应该返回第二行,因为 -3000 lca 编号的位置编号没有最新日期.

4

1 回答 1

0

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

Employee 包含主要记录。

你想找到所有的身份证号码,这样

  • idnumber 在国际
  • 最近批准开始日期的 H1BInfo 记录没有与 LCA 记录匹配的 LCA 编号

首先要做的是简化那个 H1BInfo 表。我们只寻找具有最近的approvalStartDate 的行。我们可以通过按 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)
于 2013-07-29T17:59:44.487 回答