This article is half-done without your Comment! *** Please share your thoughts via Comment ***
Check the below input data and expected output and concat id column with the duplicate name column and avoid reduce duplicate strings.
Input Data:
1 2 3 4 5 6 7 8 |
ID NAME ----------- ---------- 1 Anvesh 2 Nupur 3 dbrnd 4 dbrnd 5 Roy 6 Roy |
Expected Output:
1 2 3 4 5 6 7 8 |
ID Name ----------- ---------- 1 Anvesh 2 Nupur 3 dbrnd1 4 dbrnd2 5 Roy1 6 Roy2 |
Create a sample table with data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
CREATE TABLE tbl_DuplicateString ( ID INT ,NAME VARCHAR(10) ) GO INSERT INTO tbl_DuplicateString VALUES (1,'Anvesh') ,(2,'Nupur') ,(3,'dbrnd') ,(4,'dbrnd') ,(5,'Roy') ,(6,'Roy') GO |
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
SELECT ID ,CASE WHEN cnt > 1 THEN CONCAT(Name,rnk) ELSE NAME END Name FROM ( SELECT * , ROW_NUMBER() OVER (PARTITION BY NAME ORDER BY NAME) rnk , COUNT(*) OVER (PARTITION BY NAME) cnt FROM tbl_DuplicateString )t ORDER BY ID |
Please try the different solution for this puzzle and share it via comment...