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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:53:25+00:00 2026-05-24T08:53:25+00:00

Given a table like: id key val —- —- —– bob hair red bob

  • 0

Given a table like:

id    key   val
----  ----  -----
bob   hair  red
bob   eyes  green

And another table like:

id    key   val
----  ----  -----
fred  hair  red
fred  eyes  green
fred  shoe  42
joe   hair  red 
joe   eyes  green
greg  eyes  blue
greg  hair  brown

I’d like to find people in table b who match people in table a exactly, in this case Bob and Joe. Fred doesn’t count because he also has a shoe size. This is in Sybase so there’s no full outer join. I’ve come up with a select of a select with a union that returns people who definitely aren’t the same, but I’m not sure how to efficiently select people who are.

Alternatively, if it’s simpler, how can I check which groups in a occur in b more than once?

  • 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-24T08:53:26+00:00Added an answer on May 24, 2026 at 8:53 am

    Try this

    select a.id,b.id
    from a 
    join b on a.[key] = b.[key] and a.val = b.val -- match all rows
    join (select id,count(*) total from a group by id) a2 on a.id = a2.id -- get the total keys for table a per id
    join (select id,count(*) total from b group by id) b2 on b.id = b2.id -- get the total keys for table b per id
    group by a.id,b.id,a2.total,b2.total
    having count(*) = a2.total AND count(*) = b2.total -- the matching row's total should be equal with each tables keys per id
    

    After @t-clausen.dk comments I made a revision of the original sql code.
    In this case i count each distinct pair/value that matches on both tables, with each tables distinct pair/value.

    select td.aid,td.bid
    from (
    select a.id as aid,b.id as bid, count(distinct a.[key]+' '+a.val) total
    from a 
    join b on a.[kry] = b.[key] and a.val = b.val
    group by a.id,b.id
    ) td -- match all distinct attribute rows
    join (select id,count(distinct [key]+' '+val) total from a group by id) a2 on td.aid = a2.id -- get the total distinct keys for table a per id
    join (select id,count(distinct [key]+' '+val) total from b group by id) b2 on td.bid = b2.id -- get the total keys for table b per id
    where td.total = a2.total AND td.total = b2.total -- the matching distinct attribute total should be equal with each tables distinct key-val pair
    
    Tested on
    
    Table a
    
    bob     hair    red
    bob     eyes    green
    nick    hair    red
    nick    eyes    green
    nick    shoe    45
    
    Table b
    
    fred    hair    red
    fred    eyes    green
    joe     hair    red
    joe     eyes    green
    fred    shoe    42
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a table like: CREATE TABLE FRED ( recordId number(18) primary key,
Given a table structure like this: CREATE TABLE `user` ( `id` int(10) unsigned NOT
Given a table or a temp table, I'd like to run a procedure that
Given a table named person (in a MySQL database/schema), kind of like this one:
Given a couple of simple tables like so: create table R(foo text); create table
In C# Given a hash table like {id:'1', name:'foo'} How can I dynamically create
Given a table of id, 'points' and coordinates, would like to create a view
I have a table with a composite Primary Key, arranged something like this: CREATE
I have a composite primary key table (Maintenance Items). I would like to create
For a given table 'foo', I need a query to generate a set of

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.