Hi Friends,
Please ref my script, based on the GroupID, find the duplicate rows then merge, when VillageID Null/Zero the record can be removed.
SQL Table Script:
Create Table #T_Location(BranchID Int, ClusterID INT, ClusterName Varchar(100),VillageID INT, VillageName Varchar(100) ,GroupID INT,GroupName Varchar(100)) INSERT INTO #T_Location(BranchID, ClusterID, ClusterName, VillageID, VillageName, GroupID, GroupName) Values (100, 501, 'Neeradi Nagar', 3001, 'Defense Rx', 12, 'PPC 1991 Batch') INSERT INTO #T_Location(BranchID, ClusterID, ClusterName, VillageID, VillageName, GroupID, GroupName) Values (100, 501, 'Neeradi Nagar', '', '', 12, 'PPC 1991 Batch') INSERT INTO #T_Location(BranchID, ClusterID, ClusterName, VillageID, VillageName, GroupID, GroupName) Values (100, 503, 'PTS Ave', '', '', 15, 'IAF 1991 Batch') INSERT INTO #T_Location(BranchID, ClusterID, ClusterName, VillageID, VillageName, GroupID, GroupName) Values (100, 503, 'PTS Ave', 3006, 'X Crook', 16, 'BSF 1994 Batch') INSERT INTO #T_Location(BranchID, ClusterID, ClusterName, VillageID, VillageName, GroupID, GroupName) Values (100, 503, 'PTS Ave', '', '', 16, 'BSF 1994 Batch') INSERT INTO #T_Location(BranchID, ClusterID, ClusterName, VillageID, VillageName, GroupID, GroupName) Values (100, 507, 'OMR YX Block', '', '', 20119, 'YX V 1981 Batch') INSERT INTO #T_Location(BranchID, ClusterID, ClusterName, VillageID, VillageName, GroupID, GroupName) Values (100, 507, 'OMR YX Block', 3008, 'Zinc Force', 20120, 'YX V 1982 Batch') INSERT INTO #T_Location(BranchID, ClusterID, ClusterName, VillageID, VillageName, GroupID, GroupName) Values (100, 507, 'OMR YX Block', '', '', 20120, 'YX V 1982 Batch')
Select * from #T_Location
| BranchID | ClusterID | ClusterName | VillageID | VillageName | GroupID | GroupName | |
| 100 | 501 | Neeradi Nagar | 3001 | Defense Rx | 12 | PPC 1991 Batch | |
| 100 | 501 | Neeradi Nagar | 0 | 12 | PPC 1991 Batch | Based on the GroupID, if Village id is null or Zero, this duplicate row can be removed | |
| 100 | 503 | PTS Ave | 0 | 15 | IAF 1991 Batch | ||
| 100 | 503 | PTS Ave | 3006 | X Crook | 16 | BSF 1994 Batch | |
| 100 | 503 | PTS Ave | 0 | 16 | BSF 1994 Batch | Based on the GroupID, if Village id is null or Zero, this duplicate row can be removed | |
| 100 | 507 | OMR YX Block | 0 | 20119 | YX V 1981 Batch | ||
| 100 | 507 | OMR YX Block | 3008 | Zinc Force | 20120 | YX V 1982 Batch | |
| 100 | 507 | OMR YX Block | 0 | 20120 | YX V 1982 Batch | Based on the GroupID, if Village id is null or Zero, this duplicate row can be removed |
Expected Result:
| BranchID | ClusterID | ClusterName | VillageID | VillageName | GroupID | GroupName |
| 100 | 501 | Neeradi Nagar | 3001 | Defense Rx | 12 | PPC 1991 Batch |
| 100 | 503 | PTS Ave | 0 | 15 | IAF 1991 Batch | |
| 100 | 503 | PTS Ave | 3006 | X Crook | 16 | BSF 1994 Batch |
| 100 | 507 | OMR YX Block | 0 | 20119 | YX V 1981 Batch | |
| 100 | 507 | OMR YX Block | 3008 | Zinc Force | 20120 | YX V 1982 Batch |
Thanks in advance.