1

我正在尝试构建一个查询,该查询需要一堆数据并查找单个列 ( 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

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

4

2 回答 2

3

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

select
    Name, Age, Phone
from
    _yourtable_
where
    Phone in

        (select
            Phone
        from
            _yourtable_
        group by
            Phone
        having
            count(*) > 1
        )
于 2012-11-29T19:57:59.610 回答
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
于 2012-11-29T20:05:22.073 回答