Advertisement
alexarcan

dbd_ex1

Nov 13th, 2016
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. --b
  2. SELECT u.USERID, u.USERNAME, SUM(o.SUM) AS Total
  3. FROM USERS u, ACCOUNTS a, OPERATIONS o
  4. WHERE u.USERID = a.USERID AND
  5. a.ACC_ID = o.ACC_ID
  6. GROUP BY u.USERID, u.USERNAME
  7. HAVING SUM(o.SUM) > 100
  8. ORDER BY SUM(o.SUM) DESC;
  9.  
  10. --c)
  11. CREATE VIEW accountsView AS
  12. SELECT u.USERID, u.USERNAME, a.ACC_ID,count(o.SUM) as OPS
  13. FROM USERS u, ACCOUNTS a, OPERATIONS o
  14. WHERE u.USERID = a.USERID AND
  15. a.ACC_ID = o.ACC_ID
  16. GROUP BY u.USERID, u.USERNAME, a.ACC_ID
  17. ORDER BY count(o.SUM) DESC;
  18.  
  19. CREATE VIEW maxOPSview AS
  20. select userid, username, max(ops) as maxops
  21. from accountsview
  22. group by userid, username
  23. order by max(ops) desc;
  24.  
  25. select m.USERID, m.username, m.MAXOPS
  26. from maxopsview m, accounts a
  27. WHERE m.USERID = a.ACC_ID;
  28.  
  29.  
  30. --d)
  31. CREATE VIEW noOperationAcc AS
  32. SELECT distinct a.acc_id
  33. FROM accounts a, operations o
  34. WHERE a.ACC_ID not in
  35. (
  36. SELECT acc_id
  37. FROM OPERATIONS
  38. );
  39. DELETE FROM accounts
  40. where ACC_ID in (SELECT acc_id FROM noOperationAcc);
  41.  
  42.  
  43. DAR TRB STERS SI DIN USERS:
  44. --d)
  45. CREATE VIEW noOperationAcc AS
  46. SELECT distinct a.acc_id, a.USERID
  47. FROM accounts a, operations o
  48. WHERE a.ACC_ID not in
  49. (
  50. SELECT acc_id
  51. FROM OPERATIONS
  52. );
  53. DELETE FROM accounts
  54. where ACC_ID in (SELECT acc_id FROM noOperationAcc);
  55.  
  56. DELETE FROM users
  57. WHERE USERID in (SELECT userid FROM noOperationAcc);
  58.  
  59. SELECT * from NOOPERATIONACC;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement