简体   繁体   中英

How to search for matching staff number in sql

I am new to sql and trying to come up with a sql query which will list me the duplicate staff which were created in our system. We have one staff which is created with id as 1234 and the same user has another account starting with staff id 01234. Is there anyway i can get the matching staff Once i come up with correct duplicates i will than want to delete the accounts which don't have "0" at the start eg deleted 1234 and only keep 01234

below is the sql

SELECT     tps_user.tps_title AS [Name] , tps_user_type.tps_title AS [User Type]
FROM       tps_user INNER JOIN
                      tps_user_type ON tps_user.tps_user_type_guid = tps_user_type.tps_guid
WHERE     (tps_user.tps_title IN
                          (SELECT     tps_title AS users
                            FROM          tps_user AS t1
                            WHERE      (tps_deleted = 0)
                            GROUP BY tps_title
                            HAVING      (COUNT(tps_title) > 1))) AND (tps_user.tps_deleted = 0)

When you do you select try this:

SELECT DISTINCT CONVERT(INT,ID)
FROM your_table
WHERE ...

OR

SELECT ID
FROM your_table
WHERE ...
GROUP BY ID

This will convert all the id's to an int temporarily so when the distinct evaluates duplicates everything will be uniform to give you an accurate representation of the duplicates.

IF you don't want to convert them maybe convert them and insert them into a temporary table and add a flag to which ones have a leading zero. Or convert them then append a zero after you delete the duplicates since you want that anyway. It is easy to append a 0.

Select t1.stringId
from mytable t1
inner join mytable t2 on Convert(INT, t1.intId) = CONVERT(INT, t2.intId)
where t1.stringId not like '0%'

This should list all the persons that have duplicates but do not start with 0.

the below query will give you the list of duplicates with same Name and title. -

SELECT     tps_user.tps_title AS [Name] , 
           tps_user_type.tps_title AS [UserType], 
           COUNT(*) Duplicate_Count
FROM       tps_user 
INNER JOIN tps_user_type 
           ON tps_user.tps_user_type_guid = tps_user_type.tps_guid 
group by   tps_user.tps_title, tps_user_type.tps_title
having COUNT(*) > 1 
order by Duplicate_Count desc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM