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 6330165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:49:17+00:00 2026-05-24T17:49:17+00:00

With the following table CREATE TABLE T1 ( A varchar(2), B varchar(2) ); INSERT

  • 0

With the following table

CREATE TABLE T1 (
    A varchar(2),
    B varchar(2)
);

INSERT INTO T1 VALUES 
    ('aa', 'm'), ('aa', 'n'),
    ('bb', 'n'), ('bb', 'o'),
    ('cc', 'n'), ('cc', 'o'),
    ('dd', 'c'), ('dd', 'a'), ('dd', 'r'),
    ('ee', 'a'), ('ee', 'c'), ('ee', 'r')

A   | B
----+----
aa  | m
aa  | n
bb  | n
bb  | o
cc  | n
cc  | o
dd  | c
dd  | a
dd  | r
ee  | a
ee  | c
ee  | r

How can I select and group the values in A that match all the coresponding values in B. For example bb and cc make up a group because they both contain ‘n’ and ‘o’.

So the result would be

Group | A
----------
1     | bb
1     | cc
2     | dd
2     | ee
  • 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-24T17:49:18+00:00Added an answer on May 24, 2026 at 5:49 pm

    Here’s one approach: it first calculates the matching "sets", where a set is a group of two A‘s which match. Then it calculate the "head", or the lowest A for sets in the same group. Using dense_rank you can number the heads, and then join back on the list of sets to create a list of all set members.

    Query at SE Data.

    ; with  groups  as
            (
            select  distinct A
            from    @t
            )
    ,       vals as
            (
            select  distinct B
            from    @t
            )
    ,       sets as
            (
            select  g1.A as g1
            ,       g2.A as g2
            from    groups g1
            join    groups g2
            on      g1.A < g2.A
            cross join
                    vals v
            left join
                    @t v1
            on      g1.A = v1.A
                    and v.B = v1.B
            left join
                    @t v2
            on      g2.A = v2.A
                    and v.B = v2.B
            group by
                    g1.A
            ,       g2.A
            having  count(case when isnull(v1.B,'') <> isnull(v2.B,'') then 1 end) = 0
            )
    ,       heads as
            (
            select  s1.g1
            ,       s1.g2
            ,       head.head
            from    sets s1
            cross apply
                    (
                    select  min(g1) as head
                    from    sets s2
                    where   s1.g2 = s2.g2
                    ) head
            )
    select  distinct dense_rank() over (order by h.head)
    ,       g.g
    from    (
            select  distinct head
            from    heads
            ) h
    left join
            (
            select  g1 as g
            ,       head
            from    heads
            union all
            select  g2
            ,       head
            from    heads
            ) g
    on      h.head = g.head
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following T/SQL:- create table #temp(foo varchar(10)) insert into #temp select '' insert into
I have the following table: CREATE TABLE TEST(ID TINYINT NULL, COL1 CHAR(1)) INSERT INTO
I have the following Query: create table #Result (Reward varchar(40), Value MONEY); insert #Result
I have the following table: $sql = CREATE TABLE received_queries ( sender_screen_name varchar(50), text
I have the following database table created thus: CREATE TABLE AUCTIONS ( ARTICLE_NO VARCHAR(20),
I'm trying the following: CREATE TABLE Table1 ( RecordNo autonumber, --error here! PersonId varchar(50),
I have the following tables: CREATE TABLE title ( booktitle VARCHAR( 60 ), title_id
In the following one to many CREATE TABLE source(id int, name varchar(10), PRIMARY KEY(id));
QUESTION How can I create a MySQL table so, that a field's default value
I have the following: CREATE TABLE food_delivery ( foodtype varchar(50) NOT NULL, foodname varchar(50)

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.