匹配两个表文档类型

时间:2014-09-11 16:12:28

标签: sql sql-server sql-server-2008 sqlite

我有两张桌子。

FD_Documents
FICA11

在FD_Documents中有两列FD_CBAF8917 =文档类型和FD_B61DDE99 = ID_Number

***sample data***
2456525425625,other
2456525425625,id
2456525425625,POA
2456522456585,other
2456522456585,id
1245879566554,poa
1245879566554,ID

在FICA11

在FICA11中有两列DOC_TYPE =文件类型和ID_NUMBER = ID_Number

***sample data***
2456525425625,other
2456525425625,id
2456525425625,POA
2456522456585,other
2456522456585,id
1245879566554,poa
1245879566554,ID

现在我需要将ID号与ID号匹配,并带回文件类型上有黄金匹配的记录(含有Golden match的含义,有三种独特的文档类型,其他id,poa?

这可能吗?

谢谢

SELECT top 10 
   FD_Documents.FD_B61DDE99, 
   count(DISTINCT FD_Documents.FD_CBAF8917) as type_matches
FROM 
   FD_Documents INNER JOIN 
   FICA11 ON FD_Documents.FD_B61DDE99 = FICA11.ID_NUMBER 
          AND FD_Documents.FD_CBAF8917 = FICA11.DOC_TYPE
GROUP BY FD_Documents.FD_B61DDE99
ORDER BY count(DISTINCT FD_Documents.FD_CBAF8917) desc


    SELECT  FD_Documents.FD_B61DDE99,FD_Documents.FD_CBAF8917, FICA11.DOC_TYPE
    FROM    FD_Documents INNER JOIN
            FICA11 ON FD_Documents.FD_B61DDE99 = FICA11.ID_NUMBER

1 个答案:

答案 0 :(得分:0)

这将为您提供所有与黄金匹配的ID:

SELECT d.id 
FROM 
  FD_Documents d INNER JOIN  
  FICA11 f ON d.id = f.id AND d.type = f.type
GROUP BY d.id
HAVING COUNT(distinct d.types) = 3

要查看每个ID的输出,请尝试以下方法:

SELECT TOP 10 d.id, COUNT(distinct d.types) as type_matches
FROM 
  FD_Documents d INNER JOIN  
  FICA11 f ON d.id = f.id AND d.type = f.type
GROUP BY d.id
ORDER BY COUNT(distinct d.types) DESC