Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6931359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:34:00+00:00 2026-05-27T11:34:00+00:00

My question is how to identify duplicate (repeating) ‘groups’ of data within an SQL

  • 0

My question is how to identify duplicate (repeating) ‘groups’ of data within an SQL table. I am using SQL Server 2005 at the moment so prefer solutions based on that or ansi-sql.

Here is a sample table and expected result (below) to base this question on:

declare @data table (id nvarchar(10), fund nvarchar(1), xtype nvarchar(1))

insert into @data select 'Switch_1', 'A', 'S'
insert into @data select 'Switch_1', 'X', 'B'
insert into @data select 'Switch_1', 'Y', 'B'
insert into @data select 'Switch_1', 'Z', 'B'
insert into @data select 'Switch_2', 'A', 'S'
insert into @data select 'Switch_2', 'X', 'B'
insert into @data select 'Switch_2', 'Y', 'B'
insert into @data select 'Switch_2', 'Z', 'B'
insert into @data select 'Switch_3', 'C', 'S'
insert into @data select 'Switch_3', 'D', 'B'
insert into @data select 'Switch_4', 'C', 'S'
insert into @data select 'Switch_4', 'F', 'B'

(new data)

insert into @data select 'Switch_5', 'A', 'S'
insert into @data select 'Switch_5', 'X', 'B'
insert into @data select 'Switch_5', 'Y', 'B'
insert into @data select 'Switch_5', 'Z', 'B'

--   id         fund xtype match
--   ---------- ---- ----- ---------
--   Switch_1   A    S     Match_1
--   Switch_1   X    B     Match_1
--   Switch_1   Y    B     Match_1
--   Switch_1   Z    B     Match_1
--   Switch_2   A    S     Match_1
--   Switch_2   X    B     Match_1
--   Switch_2   Y    B     Match_1
--   Switch_2   Z    B     Match_1
--   Switch_3   C    S     
--   Switch_3   D    B     
--   Switch_4   C    S     
--   Switch_4   F    B  

(new results)

--   Switch_5   A    S     Match_1
--   Switch_5   X    B     Match_1
--   Switch_5   Y    B     Match_1
--   Switch_5   Z    B     Match_1

I only want matches on an ALL or NOTHING basis (i.e. All records in the group match all records in another group – not a part match). Any match id can be used (I have used Match_1 above but can be numeric etc.)

Thanks for any help here.

(EDIT: I guess I should add that there could be any number of rows per group, not just the 2 or 4 shown in the sample above – and I’m also trying to avoid cursors)

(EDIT 2: I seem to have an issue if there are more than one matches found. The output from the SQL supplied is returning duplicate records for Switch_1 when there are more than one matches found. I have updated the sample data accordingly. Not sure if Lieven is still following this – I’m also looking at the solution and will post here if found.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:34:00+00:00Added an answer on May 27, 2026 at 11:34 am

    The flow of execution is as follows

    • q: Combine all funds and xtypes of one id into one string using an XML PATH construction
    • r: Select a ROW_NUMBER and the respective id’s for matching groups
    • Select the results by LEFT JOINING @data and r

    SQL Statement

    ;WITH q AS (
      SELECT  DISTINCT d.id
              , DuplicateData = STUFF((SELECT ', ' + fund  + xtype FROM @data WHERE id = d.id FOR XML PATH('')), 1, 2, '')
      FROM    @data d
    ) 
    , r AS (
      SELECT  id1 = q1.id
              , id2 = q2.id
              , rn = ROW_NUMBER() OVER (ORDER BY q1.ID)
      FROM    q q1
              INNER JOIN q q2 ON q1.DuplicateData = q2.DuplicateData AND q1.id < q2.id
    )
    SELECT  id
            , fund
            , xtype
            , match = 'Match_' + CAST(r.rn AS VARCHAR(32))
    FROM    @data d
            LEFT OUTER JOIN r ON d.id IN (r.id1, r.id2)
    

    Results

    id         fund xtype match
    ---------- ---- ----- --------------------------------------
    Switch_1   A    S     Match_1
    Switch_1   X    B     Match_1
    Switch_1   Y    B     Match_1
    Switch_1   Z    B     Match_1
    Switch_2   A    S     Match_1
    Switch_2   X    B     Match_1
    Switch_2   Y    B     Match_1
    Switch_2   Z    B     Match_1
    Switch_3   C    S     NULL
    Switch_3   D    B     NULL
    Switch_4   C    S     NULL
    Switch_4   F    B     NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using SQL Server 2008. I have tables on which there are duplicate
I need a select query .. Environment : SQL DBA -SQL SERVER 2005 or
I'm working on performance tuning my SQL Server 2008 database and am using the
I have a table that stores stock ticks in sql server 2008. It is
Extending on this question: hotel reservation system SQL query: identify when one specific room
Working in SQL Server 2005, I have a stored procedure that inserts a record
After reading this on the question How do I uniquely identify computers visiting my
i've got a question, is it possible to identify the creator of a .NET
I recently asked this question: MS SQL share identity seed amongst tables (Many people
Not long ago I asked a question attempting to identify a certain unicode character

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.