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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:54:33+00:00 2026-05-13T08:54:33+00:00

I have four tables: Customer, CustomerCategory, Limit, and LimitCategory. A customer can be in

  • 0

I have four tables: Customer, CustomerCategory, Limit, and LimitCategory. A customer can be in multiple categories and a limit can also have multiple categories. I need to write a query that will return the customer name and limit amount where ALL the customers categories match ALL the limit categories.

I’m guessing it would be similar to the answer here, but I can’t seem to get it right. Thanks!

Edit – Here’s what the tables look like:

tblCustomer
  customerId
  name

tblCustomerCategory
  customerId
  categoryId

tblLimit
  limitId
  limit

tblLimitCategory
  limitId
  categoryId
  • 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-13T08:54:33+00:00Added an answer on May 13, 2026 at 8:54 am

    Updated!

    Thanks to Felix for pointing out a flaw in my existing solution (3 years after I originally posted it, hehe). After looking at it again, I think this might be correct. Here I’m getting (1) the customers and limits with matching categories, plus the number of matching categories, (2) the number of categories per customer, (3) the number of categories per limit, (4) I then ensure the number of categories for customer and limits is the same as the number of the matches between the customers and limits:

    UNTESTED!

    select
      matches.name,
      matches.limit
    
    from (
        select
          c.name,
          c.customerId,
          l.limit,
          l.limitId,
          count(*) over(partition by cc.customerId, lc.limitId) as matchCount
        from tblCustomer c
        join tblCustomerCategory cc on c.customerId = cc.customerId
        join tblLimitCategory lc on cc.categoryId = lc.categoryId
        join tblLimit l on lc.limitId = l.limitId
    ) as matches
    
    join (
        select
           cc.customerId,
           count(*) as categoryCount
         from tblCustomerCategory cc
         group by cc.customerId
    ) as customerCategories
    on matches.customerId = customerCategories.customerId
    
    join (
        select
          lc.limitId,
          count(*) as categoryCount
        from tblLimitCategory lc
        group by lc.limitId
    ) as limitCategories
    on matches.limitId = limitCategories.limitId
    
    where matches.matchCount = customerCategories.categoryCount
    and matches.matchCount = limitCategories.categoryCount
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have four DB tables in an Oracle database that need to be rewritten/refreshed
i have four tables user-question contains two columns: questionID, userID, the questions that the
I have four tables in my database and i want to join that's and
I have four tables that I am trying to join and output the result
I have four tables: RootNode // Will return multiple root nodes SubNode // Will
I have four tables. Three of them are tables with one key and they
I have four tables: Users PrivilegeGroups rdPrivileges LinkPrivilege ----------- ---------------- --------------- --------------- userId(pk) privilegeGroupId(pk)
i have four tables like below in sql server 2008 : TABLE 1 ->
I have four tables: SalesPerson ( personId int ,PersonName nvarchar(20) ..) personAndDistrict ( autoId
I have four tables in database. person(Perid ,firstname , lastname , gender) Research (Resid

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.