1
User table - user Id, first name, last name
Investment table - investment Id, investment Name
NDA table- investment Id, user Id, NDA signed(boolean)
Permission table - investment Id, user Id, view permission(boolean)

Result I need - First name and Last name of users who has NDA Signed or view permission true/false for a particular invesment Id.

Input - invesment Id

Output - first name, last name, NDA signed (true/false), view permission (true/false)

I got query where output is first name, last name , NDA signed

SELECT u.First_Name, u.Last_Name, n.Nda_Signed 
FROM user u 
JOIN nda n ON u.User_Id = n.User_Id 
JOIN investment i ON i.Investment_Id = n.Investment_Id 
WHERE i.Investment_Id =347

but having problem in joining both nda and permission table.

4
  • Why don't you have USER_ID on the investment table rather than both the NDA and PERMISSION tables? It seems redundant and more difficult to work with Jul 27, 2014 at 16:00
  • Those user_Ids are Id's of users who sign NDA(in case of NDA table) for that particular investment. There will be lots of users who sign NDA for a particular investment. NDA table is for persons who sign NDA for investments. permissions table is check whether a user is allowed to view a particular investment.
    – Davis
    Jul 27, 2014 at 16:04
  • Ah, ok. Nevermind, thought each investment would always be associated w/ just one user on both nda and permission. Jul 27, 2014 at 16:05
  • I guess what's still not clear is why NDA and permissions are separate tables.
    – Strawberry
    Jul 27, 2014 at 16:11

1 Answer 1

1

I would do this as follows:

select u.user_id,
       u.first_name,
       u.last_name,
       sum(case when typ = 'nda' and yesno = 1 then 1 else 0 end) as nda_signed,
       sum(case when typ = 'per' and yesno = 1 then 1 else 0 end) as vw_perm
  from user u
  join (select investment_id, user_id, nda_signed as yesno, 'nda' as typ
          from nda
        union all
        select investment_id, user_id, view_permission, 'per'
          from permission) x
    on u.user_id = x.user_id
where investment_id = 'xyz'
group by u.user_id,
       u.first_name,
       u.last_name

The inline view (x) merges the nda and permission tables and indicates the source on each row via the typ column. The inline view is joined back to the user table on user_id. Then you can conditionally aggregate to find which are nda_signed and view_perm 1/0.

6
  • This isn't over-complicating it ?
    – Strawberry
    Jul 27, 2014 at 16:09
  • @Strawberry it depends, the above will work even if an investment appears on the NDA table but not the PERMISSION table (or vice versa) Jul 27, 2014 at 16:11
  • Yeah - that's where I think the design is flawed
    – Strawberry
    Jul 27, 2014 at 16:12
  • @strawberry I can't disagree there, would be better to have one table with a column at the end called 'typ' indicating nda or per (basically making the actual structure of the table the way i merged both tables in the inline view) Jul 27, 2014 at 16:14
  • @BrianDeMilia the above query is working fine. Thanks.
    – Davis
    Jul 27, 2014 at 16:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.