Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Finding Duplicates Across Columns

$
0
0
Hi,I have a table that contains animal records. I need to construct a "family tree" query that returns the various generations which is fine. I then want to highlight if the same animal appears more than once in the tree i.e. in-breeding.Here is a scaled-down version of the table:[quote]CREATE TABLE [dbo].[tblHierarchy]( [ID] [int] IDENTITY(1,1) NOT NULL, [ID_Father] [int] NULL, [ID_Mother] [int] NULL, CONSTRAINT [PK_tblHierarchy] PRIMARY KEY CLUSTERED ( [ID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GO[/quote]I then create my tree query:[quote]SELECT tblHierarchy.ID ,Gen1Father.ID AS ID1_Father ,Gen2F_Father.ID AS ID2F_Father ,Gen2F_Mother.ID AS ID2F_Mother ,Gen1Mother.ID AS ID1_Mother ,Gen2M_Father.ID AS ID2M_Father ,Gen2M_Mother.ID AS ID2M_MotherFROM tblHierarchy AS Gen2M_Father RIGHT OUTER JOIN tblHierarchy AS Gen2M_Mother RIGHT OUTER JOIN tblHierarchy AS Gen1Mother ON Gen2M_Mother.ID = Gen1Mother.ID_Mother ON Gen2M_Father.ID = Gen1Mother.ID_Father RIGHT OUTER JOIN tblHierarchy ON Gen1Mother.ID = tblHierarchy.ID_Mother LEFT OUTER JOIN tblHierarchy AS Gen2F_Mother RIGHT OUTER JOIN tblHierarchy AS Gen1Father ON Gen2F_Mother.ID = Gen1Father.ID_Mother LEFT OUTER JOIN tblHierarchy AS Gen2F_Father ON Gen1Father.ID_Father = Gen2F_Father.ID ON tblHierarchy.ID_Father = Gen1Father.ID[/quote]Is there any easy way of identifying duplicates among the IDs? The idea for me would be to add an extra bit column per animal and putting in 1 of it's duplicated.Any ideas?

Viewing all articles
Browse latest Browse all 3145

Trending Articles