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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:42:56+00:00 2026-06-10T10:42:56+00:00

Imagine I have a table with data as below: ROLE_ID | USER_ID | CODE

  • 0

Imagine I have a table with data as below:

ROLE_ID | USER_ID  |  CODE
---------------------------------
 14     | USER A   |   001
 15     | USER A   |   002
 11     | USER B   |   004
 13     | USER D   |   005
 13     | USER A   |   001
 15     | USER B   |   009
 15     | USER D   |   005
 12     | USER C   |   004
 15     | USER C   |   008
 13     | USER D   |   007
 15     | USER D   |   007

I want to get the User ids and codes that only have 13 and 15 role_ids. So based on the data above I would like back the following

USER D |  005
USER D |  007

I have the query below, however, it only brings back one, not both.

   SELECT a.user_id, a.code
   FROM my_table a
   WHERE a.ROLE_ID in (13,15,11,14)
   group by a.USER_ID, a.code
    having sum( case when a.role_id in (13,15) then 1 else 0 end) = 2
    and sum( case when a.role_id in (11,14) then 1 else 0 end) = 0  
   ORDER BY USER_ID

The above query only brings

USER D |  005

rather than

USER D |  005
USER D |  007
  • 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-06-10T10:42:58+00:00Added an answer on June 10, 2026 at 10:42 am

    Sometimes just listening to your own words in English translates into the easiest to read SQL:

    SELECT DISTINCT a.user_id, a.code
       FROM my_table a
       WHERE a.user_id in 
           (SELECT b.user_id
           FROM my_table b
           WHERE b.ROLE_ID = 13)
        AND a.user_id in 
           (SELECT b.user_id
           FROM my_table b
           WHERE b.ROLE_ID = 15)
       AND a.user_id NOT IN 
           (SELECT b.user_id
           FROM my_table b
           WHERE b.ROLE_ID NOT IN (13,15))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine I have the following data in a table called messages: message_id | language_id
Imagine I have a table with two columns, a primary key and some data.
I have data class/table User that has column preferences CREATE table user; ALTER TABLE
Imagine I have following table: NAME DATE OTHER_CONTANT 'A' '2012-06-05' 'baz' 'A' '2012-06-04' 'bar'
Imagine I have this table: Column A | Column B | Column C ------------------------------
Imagine I have a table which stores a series of sparse vectors. A sparse
Imagine you have a normal table view where each row is an item on
Imagine I have these columns in a table: id int NOT NULL IDENTITY PRIMARY
Let's imagine that I have to make a table with the following structure with
Imagine we have two tables in the database, user (FK id_role) and role (PK

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.