Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Weird grouping problem thats driving my mad! Expand / Collapse
Author
Message
Posted Sunday, May 12, 2013 12:30 PM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: 2 days ago @ 4:34 AM
Points: 708, Visits: 3,292
Can somone help me with the following grouping problem?

I have a process which produced a table like the below:

create table foo(master_id int, duplicate_id int)

insert into foo
select 43157, 31574 union all
select 43157, 35731 union all
select 51477, 35731 union all
select 65842, 31574 union all
select 65842, 43157 union all
select 77822, 35731 union all
select 77822, 43157 union all
select 77822, 49202 union all
select 79673, 31574 union all
select 79673, 43157 union all
select 79673, 65842

I would like to change the result to the below:

create table foo_result (master_id int, duplicate_id int)

insert into foo_result
select 79673, 77822 union all
select 79673, 35731 union all
select 79673, 43157 union all
select 79673, 49202 union all
select 79673, 31574 union all
select 79673, 65842 union all
select 79673, 51477

Any suggestions please?











---------------------------------------------------------


It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
David Edwards - Media lens

Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
Howard Zinn
Post #1451922
Posted Sunday, May 12, 2013 2:07 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 7:09 PM
Points: 20,744, Visits: 32,560
Can you explain the logic the takes the original data to the final results?



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1451932
Posted Sunday, May 12, 2013 3:02 PM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: 2 days ago @ 4:34 AM
Points: 708, Visits: 3,292
The original table is the product of a record linkage process. All the ids are linked together. I know it's hard to spot but if you go through each row you can see how they all join together.

To simplify the processing of the next stage I would like to pick the maximum I'd and and link all other records to it.

Hope this makes sense.


---------------------------------------------------------


It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
David Edwards - Media lens

Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
Howard Zinn
Post #1451940
Posted Sunday, May 12, 2013 3:10 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 7:09 PM
Points: 20,744, Visits: 32,560
I saw they were linked, I wanted to know the logic that generated your final list. Thank you, now I have something to work with here.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1451941
Posted Sunday, May 12, 2013 3:58 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 5:42 PM
Points: 1,796, Visits: 5,805
I can't see why this is in the results?

select 79673, 49202

Can you explain?


MM


  • MMGrid Addin
  • MMNose Addin


  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • Post #1451946
    Posted Sunday, May 12, 2013 4:04 PM


    SSC-Insane

    SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

    Group: General Forum Members
    Last Login: Yesterday @ 7:09 PM
    Points: 20,744, Visits: 32,560
    mister.magoo (5/12/2013)
    I can't see why this is in the results?

    select 79673, 49202

    Can you explain?


    I am too. Only thing I can come up with is indirect relationships. Makes it difficult to work with.



    Lynn Pettis

    For better assistance in answering your questions, click here
    For tips to get better help with Performance Problems, click here
    For Running Totals and its variations, click here or when working with partitioned tables
    For more about Tally Tables, click here
    For more about Cross Tabs and Pivots, click here and here
    Managing Transaction Logs

    SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
    Post #1451947
    Posted Sunday, May 12, 2013 4:34 PM
    SSC Veteran

    SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

    Group: General Forum Members
    Last Login: Sunday, October 19, 2014 7:03 PM
    Points: 298, Visits: 712
    Hi,

    Let us know if this is the sort of thing you are after. Note that I have recursively joined back to the duplicate_id and the master_id. Hope that is how they should join.

    with cte(master_id , duplicate_id, lvl)
    as
    (
    select a.master_id, duplicate_id, 1 as lvl
    from #foo as a
    where a.master_id = (select max(master_id) from #foo)

    union all

    select c.master_id as master_id, case when c.duplicate_id = b.master_id then b.duplicate_id when c.duplicate_id = b.duplicate_id then b.master_id else null end as duplicate_id, c.lvl +1 as lvl
    from #foo as b
    inner join cte as c
    on c.duplicate_id = b.master_id or c.duplicate_id = b.duplicate_id
    and lvl<5
    )
    select distinct master_id, duplicate_id from cte
    where master_id <> duplicate_id
    OPTION (MAXRECURSION 50)



    Regards,
    Bevan Keighley
    Post #1451948
    Posted Sunday, May 12, 2013 5:03 PM


    Say Hey Kid

    Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

    Group: General Forum Members
    Last Login: 2 days ago @ 4:34 AM
    Points: 708, Visits: 3,292
    Yes, there are a number of indirect relationships and it gave me a lot of headaches.

    It's just gone past midnight here in the uk so I'm off to bed. Will try out the solution suggested first thing tomorrow morning.

    Cheers.


    ---------------------------------------------------------


    It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
    David Edwards - Media lens

    Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
    Howard Zinn
    Post #1451953
    « Prev Topic | Next Topic »

    Add to briefcase

    Permissions Expand / Collapse