Guest User

Untitled

a guest
Aug 13th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. Unique records based on same IDs with different type
  2. ID Email
  3.  
  4. 100 a@a.com
  5. 200 b@b.com
  6.  
  7. ID Assigned ID Role
  8. 100 200 Normal user
  9. 100 100 Super user
  10. 200 200 Normal user
  11.  
  12. select t1.ID,t1.email,t2.role from table t1, table t2 where t1.ID=t2.ID;
  13.  
  14. ID Email Role
  15.  
  16. 100 a@a.com Super user
  17. 100 a@a.com Normal user
  18. 200 b@b.com Normal user
  19.  
  20. ID Email Role
  21. 100 a@a.com Super user
  22. 200 b@b.com Normal user
  23.  
  24. select t1.ID,t1.email,t2.role from table t1, table t2
  25. where t1.ID=t2.ID group by t1.email having t2.role='Super user'
  26. and not exists ( select t1.ID,t1.email,t2.role from table t1, table t2
  27. where t1.ID=t2.ID group by t1.email having t2.role='Super user')
  28. UNION ALL
  29. select t1.ID,t1.email,t2.role from table t1, table t2
  30. where t1.ID=t2.ID group by t1.email having t2.role<>'Super user';
  31.  
  32. select t1.ID,t1.email,t2.role
  33. from table t1, table t2
  34. where t1.ID=t2.ID
  35. and t2.ID = t2.Assigned_ID
Add Comment
Please, Sign In to add comment