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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:33:31+00:00 2026-06-13T08:33:31+00:00

The following results from a temp table. Please Note : The data below is

  • 0

The following results from a temp table.

Please Note : The data below is just a sample so I don’t need a hardcoded query, I need a query which works for all kinds of data.

Value = 1 means the User has the role and 0 means the user has no role.
Like row one Testing 1 has Role A but not Role D.

I need a query for the below cases.

Case # 1 when Role ID = ( A,B,C,D )
I only need the user/s which have atleast one role assigned.
I should get back User Testing 1, Testing 2 and Testing 3. Testing 4 wont be coming as no role has been assigned to it.

Case # 2 When Role ID = ( A,B,C,D ) .
I only need the user/s which have all the roles assigned to a it.
I should get User Testing2 with 4 rows.

username Value Role ID
Testing1 1 A
Testing1 1 B
Testing1 1 C
Testing1 0 D
Testing2 1 A
Testing2 1 B
Testing2 1 C
Testing2 1 D
Testing3 1 A
Testing3 1 B
Testing3 1 C
Testing3 0 D
Testing4 0 A
Testing4 0 B
Testing4 0 C
Testing4 0 D

  • 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-13T08:33:31+00:00Added an answer on June 13, 2026 at 8:33 am

    CASE # 1 (which have atleast one role assigned)

    SELECT b.*
    FROM   
        (
          SELECT DISTINCT [username], Value
          FROM tableName
          WHERE value = 1
        ) a
          INNER JOIN TableName b
            ON a.username = b.username
    WHERE  a.value = 1
    

    SQLFiddle Demo

    CASE # 2 (which have all the roles assigned to a it)

    SELECT username
    FROM tableName
    WHERE RoleID IN ('A','B','C','D')
    GROUP BY username
    HAVING COUNT(DISTINCT roleID) = 4
    

    SQLFiddle Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I run the following query CREATE TABLE #Temp ( Data VARCHAR(100) ) INSERT INTO
I need a regex that will give me the following results from each example
Hi I have the following query : Player.select(Players.*, (SELECT COUNT(*) FROM Results WHERE Results.player_id
Please look the following query: SELECT ID, START, END FROM TABLEA the result is:
I am looking for some query help here is the following table data Name
I have following select results: Select t1.ID, t1.Value1 from t1 where t1.ID=1 Result: ID
what would be the result of following query? select StudentName from Students where StudentName
I have the following table >anna2 name from to result 11 66607 66841 5
I have the following temp table structure: CREATE TABLE #TempTable ( ID INT, CId
I have following data in a mysql table, +------------+--------+--------+ | date | inQty |

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.