Advertisement
Teammasik

Laba_4_BD_funcs+def

Oct 20th, 2023 (edited)
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 4.12 KB | None | 0 0
  1. --task 1
  2. create or alter function SalaryByComp(@compName varchar(30))
  3. returns table
  4. as
  5. return
  6. (
  7. select avg(salary) 'Зарплата'
  8. from Cooperator
  9. join Department on Cooperator.Dept_id = Department.Dept_id
  10. join Company on Company.Company_id = Department.Company_id
  11. where Company.Name = @compName
  12. )
  13.  
  14. select * from SalaryByComp('ПАО АНК Башнефть')
  15.  
  16. --task 2
  17. create or alter function QuantEmp(@months int)
  18. returns int
  19. as
  20. begin
  21. declare @now date = GETDATE()
  22. declare @count int
  23. set @count = (select COUNT(Cooperator.Cooperator_id)
  24.                 from Cooperator
  25.                 where DATEDIFF(MONTH, Cooperator.Start_date, @now) <= @months)
  26. return @count
  27. end
  28.  
  29. select dbo.QuantEmp(10) 'Работники'
  30.  
  31. --task 3
  32. create or alter function EmpAllGrades(@deptName varchar(30))
  33. returns table
  34. as
  35. return
  36. (
  37. select c.Name, c.Surname, c.Category
  38. from Cooperator c
  39. join Department on c.Dept_id = Department.Dept_id
  40. where Department.Name = @deptName and c.Cooperator_id in (select Cooperator.Cooperator_id
  41.                                                             from Cooperator, Evaluation
  42.                                                             where Evaluation.Cooperator_id = Cooperator.Cooperator_id and Evaluation.Status = 3)
  43.                                 and c.Cooperator_id in (select Cooperator.Cooperator_id
  44.                                                             from Cooperator, Evaluation
  45.                                                             where Evaluation.Cooperator_id = Cooperator.Cooperator_id and Evaluation.Status = 4)
  46.                                 and c.Cooperator_id in (select Cooperator.Cooperator_id
  47.                                                             from Cooperator, Evaluation
  48.                                                             where Evaluation.Cooperator_id = Cooperator.Cooperator_id and Evaluation.Status = 5)
  49. )      
  50.  
  51. select * from EmpAllGrades('IT-отдел')
  52.  
  53. --task  4
  54.  
  55. create or alter function DeptsWLow()
  56. returns table
  57. as
  58. return
  59. (
  60. select top(2) count(c.Cooperator_id) 'Количество работников', d.Name
  61. from Department d
  62. JOIN Cooperator c ON d.Dept_id = c.Dept_id
  63. GROUP BY d.Dept_id,  d.Name
  64. ORDER BY count(c.Cooperator_id)
  65. )
  66.  
  67. select * from DeptsWLow()
  68.  
  69.  
  70. --task 5
  71. create or alter function  getCoops()
  72. returns @f_coops table
  73. (
  74.     Coop_id int primary key,
  75.     Coop_name varchar(30),
  76.     Coop_surn varchar(30),
  77.     Coop_cat varchar(30),
  78.     Last_status tinyint,
  79.     Coop_res varchar(30)
  80. )
  81. as
  82. begin
  83.  
  84.     insert @f_coops
  85.     select c.Cooperator_id, c.Name, c.Surname, c.Category, e.Status, 'Годен'
  86.     from Cooperator c, Evaluation e
  87.     where (e.Date = (select max(e.Date)
  88.                     from Evaluation e
  89.                     where e.Cooperator_id = c.Cooperator_id)) and e.Status >= 3
  90.  
  91.     union
  92.     select c.Cooperator_id, c.Name, c.Surname, c.Category, e.Status, 'Уволен'
  93.     from Cooperator c, Evaluation e
  94.     where (e.Date = (select max(e.Date)
  95.                     from Evaluation e
  96.                     where e.Cooperator_id = c.Cooperator_id)) and e.Status < 3
  97.     return
  98. end
  99.  
  100. select * from dbo.getCoops()
  101.  
  102. --zashita
  103. /*
  104. посчитать сколько сотруднков у которых все оценки 5, сколько сотрудников у которых все 4
  105. на вход отдел
  106. */
  107.  
  108.  
  109. create or alter function  Coop4n5(@deptName varchar(30))
  110. returns @st_coops table
  111. (
  112.     Coop_id int,
  113.     Coop_name varchar(30),
  114.     Coop_surn varchar(30),
  115.     Coop_status varchar(30)
  116. )
  117. as
  118. begin
  119.  
  120.     insert @st_coops
  121.     select c.Cooperator_id , c.Name, c.Surname, 'все 5'
  122.     from Cooperator c, Evaluation e, Department d
  123.     where d.Name = @deptName and d.Dept_id = c.Dept_id and e.Status = 5 and e.Cooperator_id = c.Cooperator_id  and c.Cooperator_id not in (select Cooperator.Cooperator_id
  124.                                                             from Cooperator, Evaluation
  125.                                                             where Evaluation.Cooperator_id = Cooperator.Cooperator_id and Evaluation.Status <= 4)
  126.  
  127.     union
  128.     select c.Cooperator_id , c.Name, c.Surname, 'все 4'
  129.     from Cooperator c, Evaluation e, Department d
  130.     where d.Name = @deptName and d.Dept_id = c.Dept_id and e.Status = 4  and e.Cooperator_id = c.Cooperator_id  and c.Cooperator_id not in (select Cooperator.Cooperator_id
  131.                                                             from Cooperator, Evaluation
  132.                                                             where Evaluation.Cooperator_id = Cooperator.Cooperator_id and Evaluation.Status <= 3 or Evaluation.Status = 5 and Evaluation.Cooperator_id = Cooperator.Cooperator_id)
  133.  
  134.  
  135.     return
  136. end
  137.  
  138.  
  139. select * from dbo.Coop4n5('Отдел Инженеров')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement