0

I have database where 2 roles can't be associated with each other, and I need to display any users who have conflicting roles.

For example: an (id 2) accountant can't also be a (id 5) trainer

this has to be done without using CTE's

     Table a                    Table b                  table c
---------------            -------------------        ------------
userID | roleID            roleID | conflictID           roleID | Role Name

  1        2                 2           5                  1      chef
  1        3                                                2      accountant
  1        5                                                3      driver
  2        3                                                4      barmaid
  2        1                                                5      trainer
  3        2
  3        3

the result should contain only the userID who has both roles 2 and 5

userID
------
  1
3
  • What is a 'CTE'? You don't show what you tried or the structure of the tables. Commented Sep 6, 2018 at 14:39
  • 1
    @Jay Common Table Expression Commented Sep 6, 2018 at 14:41
  • @Jay: postgresql.org/docs/current/static/queries-with.html Commented Sep 6, 2018 at 14:58

1 Answer 1

1

Join the b table with the a table twice, to get userID's with conflicting combinations:

select distinct a1.userid
from tableb b
join tablea a1 on b.roleID = a1.roleID
join tablea a2 on b.conflictID = a2.roleID
              and a1.userID = a2.userID
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.