我有一张包含员工ID,账号,账户类型,银行ID和其他一些列的表BonusPay。
我需要编写在sql中返回非唯一Employee ID的查询
重复的员工ID +帐号+银行ID +帐户类型。组合必须是唯一的。
我写了查询来查找重复的员工ID
(SELECT [Employee ID],COUNT([Employee ID])
FROM [BASEP1].[INTERIM - Bonus Pay Election Enrollment]
GROUP BY [Employee ID]
HAVING COUNT([Employee ID])>1)
请帮助我进一步了解
表
**员工ID帐号银行ID号帐号类型
DC21002 1042044011 121000358 SA
DC21004 8840215401 271070801 DDA
DC21005 9942011555 21000089 DDA
DC21106 0033384728 NULL DDA
DC21117 NULL 121000358 SA
DC21053 0448921114 11000138 NULL
DC21003 4505120541 121000248 DDA
DC21007 5981285871 121000358 SA
DC21095 5400147754 121000358 SA
DC21095 0035793301 121000358 DDA
DC21044 0493540121 121000248 SA
DC21039 NULL NULL NULL
现在使用重复ID,与其他列合并。 结果看起来像
select [Employee ID]+[Account Number]+[Bank ID Number]+[Account Type]
from [BASEP1].[INTERIM - Bonus Pay Election Enrollment]
DC210021042044011121000358SA
DC210048840215401271070801DDA
DC21005994201155521000089DDA
DC21117NULL121000358SA
现在检查结果组合是否唯一
答案 0 :(得分:0)
将您的查询标识重复项放入公用表表达式,然后将重复的值加入原始表中以获得结果。
;with CTE as
(SELECT [Employee ID],COUNT([Employee ID])
FROM [BASEP1].[INTERIM - Bonus Pay Election Enrollment]
GROUP BY [Employee ID]
HAVING COUNT([Employee ID])>1)
select distinct employee ID
from [BASEP1].[INTERIM - Bonus Pay Election Enrollment] b
join cte a on a.[employee id] on b.[employee id]
答案 1 :(得分:0)
如果我理解正确(如果我错了,请纠正我),那么你想要找到组合Employee ID+Account Number+Bank ID+Account Types
的副本,然后只需按照它进行分组。这将对Employee ID+Account Number+Bank ID+Account Types
;WITH CTE
(
SELECT [Employee ID],COUNT([Employee ID]) as duplicatecount
FROM [BASEP1].[INTERIM - Bonus Pay Election Enrollment]
GROUP BY [Employee ID]
HAVING COUNT([Employee ID])>1
)
Select c.EmployeeID,
t.AccountNumber,
t.BankID,
t.AccountType
FROM
CTE c Join [BASEP1].[INTERIM - Bonus Pay Election Enrollment] t
ON c.EmployeeID = t.EmployeeID
答案 2 :(得分:0)
如果你想找到重复的行,你可以试试这个:
SELECT
[Employee ID],
[Account Number],
[Bank ID],
[Account Types],
COUNT(*)
FROM
[BASEP1].[INTERIM - Bonus Pay Election Enrollment]
GROUP BY
[Employee ID],
[Account Number],
[Bank ID],
[Account Types]
HAVING
COUNT(*) > 1
答案 3 :(得分:0)
为什么这个陈述不起作用?:
SELECT [Employee ID]
FROM [BASEP1].[INTERIM - Bonus Pay Election Enrollment]
GROUP BY [Employee ID]
HAVING COUNT([Employee ID])>1;
答案 4 :(得分:0)
这个怎么样?
;WITH CTE
AS (
SELECT *
,ROW_NUMBER() OVER (
PARTITION BY [Employee ID]
,[Account Number]
,[Bank ID]
,[Account Types]
order by [Employee ID]
) ROWNUM
FROM [BASEP1].[INTERIM - Bonus Pay Election Enrollment]
)
SELECT *
,[Employee ID] + [Account Number] + [Bank ID] + [Account Types] + ROWNUM AS [New Employee ID]
FROM CTE