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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:11:50+00:00 2026-05-25T10:11:50+00:00

I’m having a rough time figuring out the SQL query I need for a

  • 0

I’m having a rough time figuring out the SQL query I need for a situation.

I have a project that has studio level user roles for a user, and each project has project level roles that overlay/override the studio level roles. All roles are defined at the studio level, but only some roles are defined at the project level (mainly roles that have different values than their corresponding studio level role)

g_studio_UsersInRole

userId  roleId  value
1       1       TRUE
1       2       TRUE
1       3       TRUE
2       1       FALSE

g_project_UsersInRole

userId  roleId  value    projectId
1       2       FALSE    1
2       1       TRUE     1

I need a query that overlays the project roles over the studio roles for a given project Id. The tricky part is avoiding the duplicate studio level role. I need the project level roles (if any) to dominate.

I’ve been playing with Unions, but I can’t figure out how to avoid the duplicates.

Basically I need the following results:

userId  roleId  value
1       1       TRUE
1       2       FALSE
1       3       TRUE
2       1       TRUE

Where

  • userId of 1, roleId of 2 has a value False
  • userId of 2, roleId of 1 has a value True

as indicated in the project level

I thought I was close with this query, but the duplicates are still present:

;With roles As
(
SELECT     UserId, Value, RoleId
                       FROM          dbo.g_project_UsersInRole
                       WHERE      (ProjectId = 1)
                       UNION
                       SELECT     UserId, Value, RoleId
                       FROM         dbo.g_studio_UsersInRole)

SELECT     roles.RoleId, Value, UserId
FROM        roles
RIGHT JOIN (SELECT DISTINCT RoleId FROM roles) AS distinctRoles
ON distinctRoles.RoleId = roles.RoleId
  • 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-25T10:11:50+00:00Added an answer on May 25, 2026 at 10:11 am

    You don’t need a union. Just a left join to project from studio and then use coalesce

    the following

    WITH g_studio_UsersInRole AS  --Sampledata
    (
        SELECT 1  userId        ,1       roleId   , 'TRUE' value
        UNION SELECT 1,       2,       'TRUE'
        UNION SELECT 1,       3,       'TRUE'
        UNION SELECT 2,       1,       'FALSE')
    , g_project_UsersInRole as --Sampledata
    (
        SELECT 1 userId  ,       2  roleId  ,     'FALSE'  value ,     1 projectId
        UNION SELECT 2,       1,       'TRUE',     1
    )
    
    SELECT 
        sRole.userId,
        sRole.roleId,
        COALESCE(pRole.Value,sRole.value)  as value
    FROM 
        g_studio_UsersInRole sRole
        LEFT JOIN g_project_UsersInRole  pRole
        ON sRole.userId = pRole.userId
          and sRole.roleId = pRole.roleId
         and pRole.ProjectId = 1
    

    Returns the following result

    userId      roleId      value
    ----------- ----------- -----
    1           1           TRUE
    1           2           FALSE
    1           3           TRUE
    2           1           TRUE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
We're building an app, our first using Rails 3, and we're having to build
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.