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

  • Home
  • SEARCH
  • 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 3661974
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:21:52+00:00 2026-05-19T01:21:52+00:00

I have a table with 3 columns containing a variable number of records based

  • 0

I have a table with 3 columns containing a variable number of records based off of the first column which is a foreign key. I am trying to determine if I can detect when there is a duplicate across multiple rows for an entire series

declare @finddupseries table
(
    portid  int,
    asset_id int,
    allocation float
)
;
INSERT INTO @finddupseries
SELECT 250, 6, 0.05 UNION ALL
SELECT 250, 66, 0.8 UNION ALL
SELECT 250, 2, 0.105 UNION ALL
SELECT 250, 4, 0.0225 UNION ALL
SELECT 250, 5, 0.0225 UNION ALL
SELECT 251, 13, 0.6 UNION ALL
SELECT 251, 2, 0.3 UNION ALL
SELECT 251, 5, 0.1 UNION ALL
SELECT 252, 13, 0.8 UNION ALL
SELECT 252, 2, 0.15 UNION ALL
SELECT 252, 5, 0.05 UNION ALL
SELECT 253, 13, 0.4 UNION ALL
SELECT 253, 2, 0.45 UNION ALL
SELECT 253, 5, 0.15 UNION ALL
SELECT 254, 6, 0.05 UNION ALL
SELECT 254, 66, 0.8 UNION ALL
SELECT 254, 2, 0.105 UNION ALL
SELECT 254, 4, 0.0225 UNION ALL
SELECT 254, 5, 0.0225

select * from @finddupseries

The records for portid 250 and 254 match.

Is there any way I can write a query to detect this?
edit: yes, the entire series must match. Also, if there was a way to determine which one it DID match would be helpful as the actual table has around 10k records.

thanks!

  • 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-19T01:21:53+00:00Added an answer on May 19, 2026 at 1:21 am

    This query will give you all the values converted into a string grouped by port_id

    SELECT fus1.portid, 
    (
        SELECT CONVERT (VARCHAR, fus2.asset_id) + CONVERT (VARCHAR, fus2.allocation) + ','
        FROM @finddupseries fus2
        WHERE 1=1
            AND fus1.portid = fus2.portid
        ORDER BY fus2.portid, fus2.asset_id, fus2.allocation
        FOR XML PATH ('')
    ) AllValuesFromAllRows
    FROM @finddupseries fus1
    GROUP BY fus1.portid
    

    the output should look like this

    portid      AllValuesFromAllRows
    ----------- ------------------------------------------------------
    250         20.105,40.0225,50.0225,60.05,660.8,
    251         20.3,50.1,130.6,
    252         20.15,50.05,130.8,
    253         20.45,50.15,130.4,
    254         20.105,40.0225,50.0225,60.05,660.8,
    

    Now, lets do a group by with a having!

    ;With DuplicateFinder as
    (
        SELECT fus1.portid, 
        (
            SELECT CONVERT (VARCHAR, fus2.asset_id) + CONVERT (VARCHAR, fus2.allocation) + ','
            FROM @finddupseries fus2
            WHERE 1=1
                AND fus1.portid = fus2.portid
            ORDER BY fus2.portid, fus2.asset_id, fus2.allocation
            FOR XML PATH ('')
        ) AllValuesFromAllRows
        FROM @finddupseries fus1
        GROUP BY fus1.portid
    )
    SELECT AllValuesFromAllRows, COUNT (*) NumDups
    FROM DuplicateFinder
    GROUP BY AllValuesFromAllRows
    Having COUNT (*) > 1
    

    You should get

    AllValuesFromAllRows                           NumDups
    ----------------------------------------------- -----------
    20.105,40.0225,50.0225,60.05,660.8,             2
    

    So here is everything put together

    SET NOCOUNT ON
    
    declare @finddupseries table
    (
        portid  int,
        asset_id int,
        allocation float
    )
    ;
    INSERT INTO @finddupseries
    SELECT 250, 6, 0.05 UNION ALL
    SELECT 250, 66, 0.8 UNION ALL
    SELECT 250, 2, 0.105 UNION ALL
    SELECT 250, 4, 0.0225 UNION ALL
    SELECT 250, 5, 0.0225 UNION ALL
    SELECT 251, 13, 0.6 UNION ALL
    SELECT 251, 2, 0.3 UNION ALL
    SELECT 251, 5, 0.1 UNION ALL
    SELECT 252, 13, 0.8 UNION ALL
    SELECT 252, 2, 0.15 UNION ALL
    SELECT 252, 5, 0.05 UNION ALL
    SELECT 253, 13, 0.4 UNION ALL
    SELECT 253, 2, 0.45 UNION ALL
    SELECT 253, 5, 0.15 UNION ALL
    SELECT 254, 6, 0.05 UNION ALL
    SELECT 254, 66, 0.8 UNION ALL
    SELECT 254, 2, 0.105 UNION ALL
    SELECT 254, 4, 0.0225 UNION ALL
    SELECT 254, 5, 0.0225
    
    ;With PivotAssetIdAndAllocation as
    (
        SELECT fus1.portid, 
        (
            SELECT CONVERT (VARCHAR, fus2.asset_id) + '_'+ CONVERT (VARCHAR, fus2.allocation) + '~~'
            FROM @finddupseries fus2
            WHERE 1=1
                AND fus1.portid = fus2.portid
            ORDER BY fus2.portid, fus2.asset_id, fus2.allocation
            FOR XML PATH ('')
        ) AllValuesFromAllRows
        FROM @finddupseries fus1
        GROUP BY fus1.portid
    )
    ,
    ListOfDuplicates AS
    (
        SELECT AllValuesFromAllRows, COUNT (*) NumDups
        FROM PivotAssetIdAndAllocation
        GROUP BY AllValuesFromAllRows
        Having COUNT (*) > 1
    )
    SELECT portid, AllValuesFromAllRows
    FROM PivotAssetIdAndAllocation
    WHERE AllValuesFromAllRows IN (SELECT AllValuesFromAllRows FROM ListOfDuplicates)
    

    and the output is

    portid      AllValuesFromAllRows
    ----------- ----------------------------------------------------------------------
    250         2_0.105~~4_0.0225~~5_0.0225~~6_0.05~~66_0.8~~
    254         2_0.105~~4_0.0225~~5_0.0225~~6_0.05~~66_0.8~~
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with two columns. The first (which contains a menu) should
I have a fixed width DIV containing a table with many columns, and need
I have a table y Which has two columns a and b Entries are:
I have a table with say 3 columns. There's no primary key so there
I have a java class containing all the columns of a database table as
I have a table containing a large number of rows. Each row has 2
I have a table containing a large number of rows. Each row has 2
I have (adjusted for the sake of simplicity) a table containing 2 columns -
I have a table table_a with a column containing data as follows: .aaa01932 .asd02343
I have a JTable instance, containing a number of rows. The columns in this

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.