在SQL中计算条件匹配

时间:2013-01-15 00:58:07

标签: sql select join

问题:

在Person表中查找匹配记录的数量,其中名字和姓氏相同,或者SSN是相同的。

GIVENS

表格结构:

FirstName
LastName
SSN
RecordId
人员表中

DATA:

╔═══════════╦══════════╦═════════════╦══════════╗
║ FIRSTNAME ║ LASTNAME ║     SSN     ║ RECORDID ║
╠═══════════╬══════════╬═════════════╬══════════╣
║ Bob       ║ Smith    ║ 111-11-1111 ║        1 ║
║ Robert    ║ Smyth    ║ 111-11-1111 ║        2 ║
║ Bob       ║ Smith    ║ 222-22-2222 ║        3 ║
║ Tom       ║ Gunn     ║ 333-33-3333 ║        4 ║
║ Jim       ║ Gunn     ║ 444-44-4444 ║        5 ║
║ Jimmy     ║ Gunn     ║ 444-44-4444 ║        6 ║
║ Alice     ║ Jones    ║ 555-55-5555 ║        7 ║
║ Alice     ║ Jones    ║ 666-66-6666 ║        8 ║
╚═══════════╩══════════╩═════════════╩══════════╝

期望的结果:

╔════════════╦═══════════╦══════════╦═════════════╦══════════╗
║ MATCHCOUNT ║ FIRSTNAME ║ LASTNAME ║     SSN     ║ RECORDID ║
╠════════════╬═══════════╬══════════╬═════════════╬══════════╣
║          3 ║ Bob       ║ Smith    ║ 111-11-1111 ║        1 ║
║          2 ║ Robert    ║ Smyth    ║ 111-11-1111 ║        2 ║
║          2 ║ Bob       ║ Smith    ║ 222-22-2222 ║        3 ║
║          1 ║ Tom       ║ Gunn     ║ 333-33-3333 ║        4 ║
║          2 ║ Jim       ║ Gunn     ║ 444-44-4444 ║        5 ║
║          2 ║ Jimmy     ║ Gunn     ║ 444-44-4444 ║        6 ║
║          2 ║ Alice     ║ Jones    ║ 555-55-5555 ║        7 ║
║          2 ║ Alice     ║ Jones    ║ 666-66-6666 ║        8 ║
╚════════════╩═══════════╩══════════╩═════════════╩══════════╝

我尝试了什么

我尝试过使用GROUP BY和LEFT self JOIN来解决这个问题。

GROUP BY失败,因为无法有条件地分组。

LEFT self JOIN失败,因为它大致返回匹配数的平方。

我会发布我尝试的代码,但是我的真实表格有很多列,实际上要复杂得多,而且几乎肯定会模糊这个问题。

1 个答案:

答案 0 :(得分:3)

因为你还没有提到过RDBMS,试试这个,

SELECT  COUNT(*) MatchCount,a.*
FROM    person a
        INNER JOIN person b
            ON  (a.firstname = b.firstname AND
                    a.lastname = b.lastname) OR
                    a.SSN = b.SSn
GROUP BY firstname, lastname, ssn, recordid
ORDER By recordid

<强>结果

| MATCHCOUNT | FIRSTNAME | LASTNAME |         SSN | RECORDID |
--------------------------------------------------------------
|          3 |       Bob |    Smith | 111-11-1111 |        1 |
|          2 |    Robert |    Smyth | 111-11-1111 |        2 |
|          2 |       Bob |    Smith | 222-22-2222 |        3 |
|          1 |       Tom |     Gunn | 333-33-3333 |        4 |
|          2 |       Jim |     Gunn | 444-44-4444 |        5 |
|          2 |     Jimmy |     Gunn | 444-44-4444 |        6 |
|          2 |     Alice |    Jones | 555-55-5555 |        7 |
|          2 |     Alice |    Jones | 666-66-6666 |        8 |

SQL Fiddle