Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

How to find duplicate records merge in MS.SQL

$
0
0

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

BranchIDClusterIDClusterNameVillageIDVillageNameGroupIDGroupName
100501Neeradi Nagar3001Defense Rx12PPC 1991 Batch
100501Neeradi Nagar0 12PPC 1991 BatchBased on the GroupID, if Village id is null or Zero, this duplicate row can be removed 
100503PTS Ave0 15IAF 1991 Batch
100503PTS Ave3006X Crook16BSF 1994 Batch
100503PTS Ave0 16BSF 1994 BatchBased on the GroupID, if Village id is null or Zero, this duplicate row can be removed 
100507OMR YX Block0 20119YX V 1981 Batch
100507OMR YX Block3008Zinc Force20120YX V 1982 Batch
100507OMR YX Block0 20120YX V 1982 BatchBased on the GroupID, if Village id is null or Zero, this duplicate row can be removed 

Expected Result:

BranchIDClusterIDClusterNameVillageIDVillageNameGroupIDGroupName
100501Neeradi Nagar3001Defense Rx12PPC 1991 Batch
100503PTS Ave0 15IAF 1991 Batch
100503PTS Ave3006X Crook16BSF 1994 Batch
100507OMR YX Block0 20119YX V 1981 Batch
100507OMR YX Block3008Zinc Force20120YX V 1982 Batch

Thanks in advance. 


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>