After a lot of searching, it seems most relational division problems target groups with matching members. Maybe I'm having keyword trouble, but I want something a little different: given a parent/group and set of children/members, how do I find each unique combination of members, regardless of parent?

Using the following sample source

CREATE TABLE #m (Parent char(1), ChildID int)

INSERT INTO #m
VALUES ('a',1), ('a', 2), ('a',4),
       ('b',1), ('b', 3), ('b',4),
       ('c',1), ('c', 4), ('c',2),
       ('d',1), ('d',4),
       ('e',3), ('e', 1),
       ('f',4),
       ('g',3), ('g', 4), ('g',1);

SELECT * FROM #m

I'd be looking for a result like (1, 2, 4), (1, 3, 4), (1, 4), (1, 3), (4), expressed as a new temp table (to join back to #m, so that each Parent can be pointed to its "hash" rather than its matching Parent)

There are lots of syntax variations on this stuff; this kind makes the most sense to me, but hasn't gotten me to an answer. Apologies for duplication I can't find.

EDIT: the desired result expressed as a SQL resultset:

UParent ChildID
------- -----------
u1      1
u1      2
u1      4
u2      1
u2      3
u2      4
u3      1
u3      4
u4      1
u4      3
u5      4
有帮助吗?

解决方案 2

I see, you want the "unique" combinations of children, regardless of order.

The following gets parents that are equivalent:

select m1.Parent as Parent1, m2.Parent as Parent2
from (select m.*, count(*) over (partition by Parent) as NumKids
      from #m m
     ) m1 join
     (select m.*, count(*) over (partition by Parent) as NumKids
      from #m m
     ) m2
     on m1.ChildID = m2.ChildID
group by m1.Parent, m2.Parent
having count(*) = max(m1.NumKids) and max(m1.NumKids) = max(m2.NumKids);

We can now get what you want using this

with parents as (
    select m1.Parent as Parent1, m2.Parent as Parent2
    from (select m.*, count(*) over (partition by Parent) as NumKids
          from #m m
         ) m1 join
         (select m.*, count(*) over (partition by Parent) as NumKids
          from #m m
         ) m2
         on m1.ChildID = m2.ChildID
    group by m1.Parent, m2.Parent
    having count(*) = max(m1.NumKids) and max(m1.NumKids) = max(m2.NumKids)
)
select distinct m.*
from (select min(Parent2) as theParent
      from parents
      group by Parent1
     ) p join
     #m m
     on p.theParent = m.Parent;

If you want a new id instead of the old one, use:

select dense_rank() over (partition by m.Parent) as NewId, m.ChildID

in the select.

其他提示

This uses for xml to build a sorted child id list that is used as a partition clause of the rank() function.

select M.Parent,
       M.ChildID
from (
     select M1.Parent,
            M1.ChildID,
            rank() over(partition by (
                                     select cast(ChildID as varchar(11))+','
                                     from #m as M2
                                     where M1.Parent = M2.Parent
                                     order by M2.ChildID
                                     for xml path('')
                                     )
                        order by M1.Parent) as rn
     from #m as M1
     ) as M
where M.rn = 1;

SQL Fiddle

Correct me if I am wrong the way your data is stored isnt it already somewhat in the format what you want it to be.

I tried this query from your given sample data and it produces the result set you are expecting. have a look here

SELECT 'u' + CAST(DENSE_RANK() OVER (ORDER BY Parent) AS VARCHAR(10)) AS UParent
      ,ChildID
FROM #m

Result Set

╔═════════╦═════════╗
║ UParent ║ ChildID ║
╠═════════╬═════════╣
║ u1      ║       1 ║
║ u1      ║       2 ║
║ u1      ║       4 ║
║ u2      ║       1 ║
║ u2      ║       3 ║
║ u2      ║       4 ║
║ u3      ║       1 ║
║ u3      ║       4 ║
║ u3      ║       2 ║
║ u4      ║       1 ║
║ u4      ║       4 ║
║ u5      ║       3 ║
║ u5      ║       1 ║
║ u6      ║       4 ║
║ u7      ║       3 ║
║ u7      ║       4 ║
║ u7      ║       1 ║
╚═════════╩═════════╝
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top