请使用我的SQL Server查询获得一些帮助。
我有一个SQL Server表,如果Employee_Number
列有多个记录,我想获取所有列数据;如果它只有唯一的价值,那么我不想让它显示出来。
从下面的示例中,Employee_Number
列的重复值为1111111
和3333333
。因此它显示了重复值的所有记录,包括所有其余列值。
下面粘贴的数据看起来很分散,所以我也显示为图像(我的数据和预期输出)
SQL Server中的实际数据:
userid Gecos B2A_LDAP AD Employee_Number B2A_CN absJobAction Generic_OR_User Unix_ID Additional_ID_in_Unix Application_Access
user1 user one user1 user1 1111111 user one HIRED User user1 NULL NULL
user2 user two user2 user2 2222222 user two HIRED User user2 NULL NULL
user11 user one additional user1 user1 1111111 user one HIRED User user11 NULL NULL
user3 user three user3 user3 3333333 user three HIRED User user3 NULL NULL
user33 user three spare ID 1 user3 user3 3333333 user three HIRED User user33 NULL NULL
user4 user four user4 user4 4444444 user four HIRED User user4 NULL NULL
user333 user three spare ID 3 user3 user3 3333333 user three HIRED User user333 NULL NULL
我的预期输出应如下所示。
userid Gecos B2A_LDAP AD Employee_Number B2A_CN absJobAction Generic_OR_User Unix_ID Additional_ID_in_Unix Application_Access
user1 user one user1 user1 1111111 user one HIRED User user1 NULL NULL
user11 user one additional user1 user1 1111111 user one HIRED User user11 NULL NULL
user3 user three user3 user3 3333333 user three HIRED User user3 NULL NULL
user33 user three spare ID 1 user3 user3 3333333 user three HIRED User user33 NULL NULL
user333 user three spare ID 3 user3 user3 3333333 user three HIRED User user333 NULL NULL
答案 0 :(得分:1)
您可以尝试此操作来查找重复Employee_Number
SELECT *
FROM YourUserTable y
WHERE Employee_Number IN ( SELECT y1.Employee_Number
FROM YourUserTable y1
GROUP BY y1.Employee_Number
HAVING count(*) > 1
)
答案 1 :(得分:0)
尝试此查询, 如果userId列在表中是唯一的,它将起作用。
SELECT *
FROM UserMst
WHERE EmployeeNumber IN ( SELECT EmployeeNumber
FROM UserMst
GROUP BY EmployeeNumber
HAVING COUNT(*) > 1)
答案 2 :(得分:0)
select top 10 *
from Shipping_Bill a
join
(select sb_number
from Shipping_Bill
group by SB_Number
having count(*) > 1) b on a.SB_Number = b.SB_Number
请尝试此查询。它会对你有所帮助。