比较两行SQL Server

时间:2012-11-29 19:48:30

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

我正在尝试构建一个查询,该查询获取一堆数据并查找单个列(PHONE)的重复项,然后输出整行。我已经成功地将我的数据池缩小到4个结果(这是正确的)。现在我也试图显示重复的行,所以例如我想看到如下内容:

+--------+-------+--------------+
|  Name  |  Age  |  Phone       |
|  Abby  |  20   | 123-456-7890 |
|  Mike  |  42   | 123-456-7890 |
|  Abel  |  32   | 123-555-0000 |
|  John  |  24   | 123-555-0000 |
+--------+-------+--------------+

但是我的查询输出的内容如下:

+--------+-------+--------------+
|  Name  |  Age  |  Phone       |
|  Abby  |  20   | 123-456-7890 |
|  Abel  |  32   | 123-555-0000 |
+--------+-------+--------------+

我如何展示Mike和John以及Abby和Abel?

我目前的查询:

SELECT DISTINCT C.ACCOUNT,
                C.ADATE,
                C.ATIME,
                C.PHONE,
                C.PATIENTID,
                C.RN
FROM   (SELECT A.ACCOUNT,
               A.ADATE,
               A.ATIME,
               M.HPHONE,
               Row_number()
                 OVER (
                   PARTITION BY M.HPHONE
                   ORDER BY M.HPHONE) AS RN
        FROM   MWAPPTS A
               JOIN CLMASTER M
                 ON A.ACCOUNT = M.ACCOUNT
        WHERE  ( ADATE >= '2012-11-30 00:00:00.000'
                 AND ADATE <= '2012-12-03 00:00:00.000' )
               AND DEPARTMENT LIKE '%bowie%'
               AND A.USERFLAG IN ( 'U' )) RESULTS(ACCOUNT, ADATE, ATIME, PHONE, RN)
       JOIN (SELECT A.ACCOUNT,
                    A.ADATE,
                    A.ATIME,
                    M.HPHONE,
                    A.PATIENTID,
                    Row_number()
                      OVER (
                        PARTITION BY M.HPHONE
                        ORDER BY M.HPHONE) AS RN
             FROM   MWAPPTS A
                    JOIN CLMASTER M
                      ON A.ACCOUNT = M.ACCOUNT
             WHERE  ( ADATE >= '2012-11-30 00:00:00.000'
                      AND ADATE <= '2012-12-03 00:00:00.000' )
                    AND DEPARTMENT LIKE '%bowie%'
                    AND A.USERFLAG IN ( 'U' )) C(ACCOUNT, ADATE, ATIME, PHONE, PATIENTID, RN)
         ON RESULTS.ACCOUNT = C.ACCOUNT
WHERE C.RN <> 1
ORDER  BY C.PHONE,
          C.RN

提前感谢您提供的任何帮助。

2 个答案:

答案 0 :(得分:3)

您的示例代码中有很多字段未在示例输出中引用,因此我只是从头开始,只使用示例中的字段:

select
    Name, Age, Phone
from
    _yourtable_
where
    Phone in

        (select
            Phone
        from
            _yourtable_
        group by
            Phone
        having
            count(*) > 1
        )

答案 1 :(得分:0)

这样的事情应该有效:

With r as (
  Select
    a.Account,
    a.ADate,
    a.Atime,
    m.HPhone,
    a.PatientID
  From
    MwAppts a
      Inner Join
    ClMaster m
      On a.Account = m.Account
  Where
    ADate >= '2012-11-30' And
    ADate <= '2012-12-03' And -- it's generally considered better to do < endday + 1, as this works if you have datetime fields too
    Department Like '%bowie%' And
    a.UserFlag In ( 'U' )
)

Select Distinct -- Might not need distinct
  r.Account,
  r.ADate,
  r.ATime,
  r.HPhone,
  r.PatientID
From (
    Select
      HPhone
    From
      r
    Group By
      HPhone
    Having
      Count(*) > 1
    ) dupPhones
    Inner Join
  r 
    On dupPhones.HPhone = r.HPhone
Order By
  r.HPhone