Advertisement
Teammasik

laba_2_BD_views

Oct 6th, 2023 (edited)
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 2.88 KB | None | 0 0
  1. --1
  2. CREATE VIEW fir_task AS
  3. SELECT City, COUNT(Name) AS 'Кол-во компаний'
  4. FROM Company
  5. GROUP BY City
  6.  
  7. SELECT *
  8. FROM fir_task
  9.  
  10. DROP VIEW fir_task
  11.  
  12. --2
  13. CREATE VIEW sec_task AS
  14. SELECT Cooperator.Name AS 'имя', Cooperator.Surname, Department.Name AS 'отдел', Company.Name AS 'компания'
  15. FROM Cooperator, Department, Company
  16. WHERE Company.Coefficent > 6 AND Cooperator.Dept_id = Department.Dept_id AND Department.Company_id = Company.Company_id
  17.  
  18. SELECT * FROM sec_task
  19.  
  20. DROP VIEW sec_task
  21.  
  22. --3
  23. CREATE VIEW trd_task AS
  24. SELECT Department.Name AS 'отдел', AVG(Evaluation.STATUS) AS 'среднее'
  25. FROM Department --, Evaluation
  26. JOIN Cooperator ON Cooperator.Dept_id = Department.Dept_id
  27. JOIN Evaluation ON Evaluation.Cooperator_id = Cooperator.Cooperator_id
  28. GROUP BY Department.Name
  29. HAVING AVG(Evaluation.STATUS) > 3;
  30.  
  31. SELECT * FROM trd_task
  32.  
  33. DROP VIEW trd_task
  34.  
  35. --4
  36. CREATE VIEW fourth_task AS
  37. SELECT Cooperator.name, Cooperator.Surname, Cooperator.Salary
  38. FROM Cooperator ,(SELECT AVG(salary)ss, Department.Dept_id
  39.                     FROM Department, Cooperator
  40.                     WHERE Department.Dept_id = Cooperator.Dept_id
  41.                     GROUP BY Department.Dept_id) t
  42. WHERE Cooperator.Dept_id = t.Dept_id AND Cooperator.Salary > t.ss
  43.  
  44. SELECT * FROM fourth_task
  45.  
  46. DROP VIEW fourth_task
  47.  
  48. --5
  49. CREATE VIEW fifth_task AS
  50. SELECT Cooperator.Name, Cooperator.Surname, Company.City
  51. FROM Cooperator, Company, Department
  52. WHERE Cooperator.City=Company.City AND Department.Company_id=Company.Company_id AND Cooperator.Dept_id = Department.Dept_id
  53.  
  54. SELECT * FROM fifth_task
  55.  
  56. DROP VIEW fifth_task
  57.  
  58. --6
  59. CREATE VIEW sixth_task AS
  60. SELECT Name, Surname
  61. FROM Cooperator
  62. JOIN Evaluation ON Cooperator.Cooperator_id = Evaluation.Cooperator_id
  63. WHERE STATUS = 5 WITH CHECK OPTION
  64.  
  65. SELECT * FROM sixth_task
  66.  
  67. DROP VIEW sixth_task
  68.  
  69. --7
  70. CREATE VIEW seventh_task AS
  71. SELECT Name, Short_name AS 'аббревиатура', Coefficent
  72. FROM Company
  73. WHERE Coefficent <=5 AND Coefficent >=0 WITH CHECK OPTION
  74.  
  75. SELECT * FROM seventh_task
  76.  
  77. --INSERT INTO seventh_task (Name, аббревиатура, Coefficent) это проверка, при выполнении должна
  78. --VALUES('Globe inc', 'Globe', 10)                          упасть с ошибкой
  79.  
  80. DROP VIEW seventh_task
  81.  
  82. -- zashita
  83. -- task: вывести тех сотрудников у которых последняя оценка была 5
  84. CREATE OR ALTER  VIEW viewpr
  85. AS
  86. SELECT Cooperator.Cooperator_id, Cooperator.Name, Evaluation.STATUS
  87. FROM Cooperator, Evaluation, (SELECT Evaluation.DATE, Cooperator.Cooperator_id
  88.                                 FROM Cooperator, Evaluation
  89.                                 WHERE Cooperator.Cooperator_id = Evaluation.Cooperator_id) t
  90. WHERE Cooperator.Cooperator_id = t.Cooperator_id AND  Evaluation.STATUS = 5
  91. GROUP BY Evaluation.DATE, Cooperator.Name, Evaluation.STATUS, Cooperator.Cooperator_id
  92. HAVING Evaluation.DATE = MAX(t.DATE)
  93.  
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement