2013-07-29 118 views
0

我有3個表,我試圖創建一個查詢:
表1(iuieEmployee) - >位置編號
表2(jbEmployeeH1BInfo) - >位置號碼,LCA號,開始日期
表3(jbEmployeeLCA) - > LCA數
表4(jbInternationsl) - >主人口統計表

需要使用SQL查詢援助

我有一個查詢那裏只有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,2013年7月30日, '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編號的位置編號沒有最新的日期。

+0

您使用的是什麼RDBMS? –

+0

MS SQL Server 2008 R2 –

回答

0

你的解釋很難理解。我想如果你能解釋得很好,那麼你可以自己編寫查詢。以下是我認爲你的意思:

員工包含主要記錄。

你想找到所有idnumbers這樣

  • 的IDNumber是國際
  • 的H1BInfo紀錄最近approvalStartDate沒有匹配的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) 
+0

這給了我相同的結果,因爲我的查詢越來越。問題是如果一個特定的記錄在每個表中有1個位置但是2個lca數字並且沒有匹配,則該記錄返回兩次。如果沒有與最近開始日期的lca記錄相匹配,我只想查看每個id。 –

+0

@LaurenRobinson我不確定你的意思。你能用示例模式和數據創建一個SQL小提琴來說明你的觀點嗎? –