我正在尝试构建一个查询,该查询需要一堆数据并查找单个列 ( 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
提前感谢您提供的任何帮助。