我真的很难用SQL查询来只返回重复的行,例如:
EMPLOYEE_ID STORE_NO REGION
2531047 1671 E05
2544970 1254 W09
2566618 1625 E01
2566618 1635 E01
2566618 1835 E01
Should look like:
EMPLOYEE_ID STORE_NO REGION
2566618 1625 E01
2566618 1635 E01
2566618 1835 E01
以上是通过以下方式实现的:
SELECT
E.EMPLOYEE_ID,
E.STORE_NO,
S.REGION
FROM
[USICOAL].[dbo].[EMPLOYEE_STR_ASGN] AS E
RIGHT JOIN
[USICOAL].[dbo].[STORE] AS S ON E.STORE_NO = S.STORE_NO
ORDER BY EMPLOYEE_ID, REGION
我尝试了各种选项,但只能找到一种方法来删除这些重复项,而不是仅显示它们......任何人都可以建议上述内容是否可行?
提前致谢, 查理。
答案 0 :(得分:0)
SELECT * FROM stuff st
WHERE EXISTS (
SELECT 1 FROM stuff xx
WHERE xx.employee_id = st.employee_id
GROUP BY xx.employee_id
HAVING COUNT(*) > 1
)
;
答案 1 :(得分:0)
SELECT
E.EMPLOYEE_ID,
E.STORE_NO,
S.REGION
FROM
[USICOAL].[dbo].[EMPLOYEE_STR_ASGN] AS E
RIGHT JOIN
[USICOAL].[dbo].[STORE] AS S ON E.STORE_NO = S.STORE_NO
JOIN
( SELECT EMPLOYEE_ID
FROM [USICOAL].[dbo].[EMPLOYEE_STR_ASGN]
GROUP BY EMPLOYEE_ID
HAVING COUNT(*) > 1 ) DUPS
ON DUPS.EMPLOYEE_ID = E.EMPLOYEE_ID
ORDER BY E.EMPLOYEE_ID, REGION
正确的加入可能只是一个加入 如果有> 1名员工有> 0名员工