Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. CREATE TABLE Patients
  2. (
  3. ID INT IDENTITY (1, 1) NOT NULL,
  4. Surname NVARCHAR(50) NOT NULL,
  5. BirthDate DATE NOT NULL,
  6. PhoneNumber NVARCHAR(15),
  7. PRIMARY KEY (ID)
  8. );
  9.  
  10.  
  11. INSERT INTO Patients VALUES ('Замотаев', '1981-12-09', '+78923432');
  12. INSERT INTO Patients VALUES ('Кошкин', '1998-11-07', '+7854432');
  13. INSERT INTO Patients VALUES ('Собачкин', '2001-12-09', '+7812432');
  14. INSERT INTO Patients VALUES ('Дроид', '1961-12-09', '+777732');
  15. INSERT INTO Patients VALUES ('БибиВосемь', '2014-10-03', '+8888');
  16. INSERT INTO Patients VALUES ('КаДевять', '1968-12-09', '+9999');
  17.  
  18. CREATE TABLE Price
  19. (
  20. ServiceID INT IDENTITY (1, 1) NOT NULL,
  21. Name NVARCHAR(50) NOT NULL,
  22. DoctorID INT NOT NULL,
  23. cost INT NOT NULL,
  24. PRIMARY KEY (ServiceID)
  25. );
  26.  
  27. INSERT INTO Price VALUES ('Консультация', 1, 1500);
  28. INSERT INTO Price VALUES ('Консультация', 5, 120000);
  29. INSERT INTO Price VALUES ('Первичный прием', 2, 1100);
  30. INSERT INTO Price VALUES ('Вторичный прием', 3, 5000);
  31. INSERT INTO Price VALUES ('Осмотр', 4, 4500);
  32.  
  33. CREATE TABLE Payments
  34. (
  35. PaymentID INT IDENTITY (1, 1) NOT NULL,
  36. PatientID INT NOT NULL,
  37. PriceID INT NOT NULL,
  38. PaymentDate DATETIME,
  39. PRIMARY KEY (PaymentID)
  40. );
  41.  
  42. INSERT INTO Payments VALUES (8, 5, '18-06-12 10:40:09 PM');
  43. INSERT INTO Payments VALUES (8, 1, '18-06-12 10:41:09 PM');
  44. INSERT INTO Payments VALUES (8, 2, '18-06-12 10:42:09 PM');
  45. INSERT INTO Payments VALUES (8, 3, '18-06-12 10:43:09 PM');
  46. INSERT INTO Payments VALUES (8, 4, '18-06-12 10:44:09 PM');
  47. INSERT INTO Payments VALUES (7, 3, '18-06-12 10:44:09 PM');
  48.  
  49.  
  50. CREATE TABLE Visits
  51. (
  52. VisitID INT IDENTITY (1, 1) NOT NULL,
  53. PatientID INT NOT NULL,
  54. DoctorID INT NOT NULL,
  55. VisitingTime DATETIME NOT NULL,
  56. Durability TIME NOT NULL,
  57. PRIMARY KEY (VisitID)
  58. );
  59. INSERT INTO Visits VALUES (8, 6, '18-06-12 10:50:00 PM', '00:40')
  60. INSERT INTO Visits VALUES (8, 1, '18-06-17 10:50:00 PM', '00:40')
  61. INSERT INTO Visits VALUES (1, 2, '18-06-17 10:50:00 PM', '00:40')
  62.  
  63.  
  64.  
  65. CREATE TABLE Departments
  66. (
  67. ID INT IDENTITY (1, 1) NOT NULL,
  68. OpeningTime TIME NOT NULL,
  69. ClosingTime TIME NOT NULL,
  70. PRIMARY KEY (ID)
  71. );
  72.  
  73. INSERT INTO Departments VALUES ('08:00', '17:00');
  74. INSERT INTO Departments VALUES ('08:00', '20:00');
  75. INSERT INTO Departments VALUES ('08:00', '19:00');
  76.  
  77. CREATE TABLE Doctors
  78. (
  79. ID INT IDENTITY (1, 1) NOT NULL,
  80. DepatramentID INT NOT NULL,
  81. Surname NVARCHAR(50) NOT NULL,
  82. Specialty NVARCHAR(50) NOT NULL,
  83. PhoneNumber NVARCHAR(15),
  84. PRIMARY KEY (ID)
  85. );
  86.  
  87. INSERT INTO Doctors VALUES (1, 'Иванова', 'Окулист', '88888');
  88. INSERT INTO Doctors VALUES (1, 'Петрова', 'Окулист', '89023');
  89. INSERT INTO Doctors VALUES (2, 'Лифанов', 'Психотерапевт', '31227');
  90. INSERT INTO Doctors VALUES (3, 'Немцев', 'Стоматолог', '333');
  91. INSERT INTO Doctors VALUES (3, 'Чудакова', 'Хирург', '2394023');
  92. INSERT INTO Doctors VALUES (2, 'Замотаева', 'Окулист', '443572');
  93. INSERT INTO Doctors VALUES (1, 'Уволенный', 'Окулист', '423572');
  94.  
  95.  
  96.  
  97. --------------------------------1-------------------------------------
  98.  
  99. ALTER FUNCTION GetTimetable (@y INT, @m INT, @d INT, @dID INT)
  100. RETURNS TABLE
  101. RETURN SELECT
  102. v.VisitingTime,
  103. p.Surname
  104. FROM Doctors d
  105. JOIN Visits v ON d.ID = v.DoctorID
  106. JOIN Patients p ON p.ID = v.PatientID
  107. WHERE MONTH(v.VisitingTime) = @m AND YEAR(v.VisitingTime) = @y AND DAY(v.VisitingTime) = @d
  108. AND v.DoctorID = @dID
  109. GROUP BY v.VisitingTime, p.Surname
  110.  
  111. SELECT *
  112. FROM GetTimetable(2012, 6, 18, 5)
  113.  
  114. -------------------------------2--------------------------------------
  115. SELECT
  116. d.Specialty,
  117. d.Surname,
  118. COUNT(d.Surname)
  119. FROM
  120. Doctors d
  121. JOIN Visits v ON v.DoctorID = d.ID
  122. GROUP BY d.Specialty, d.Surname
  123.  
  124. ------------------------------3----------------------------------------
  125.  
  126.  
  127. SELECT d.Specialty, sum(pr.cost)
  128. FROM Doctors d
  129. JOIN Price pr ON d.ID = pr.DoctorID
  130. JOIN Payments p on p.PriceID=pr.ServiceID
  131. GROUP BY d.Specialty
  132. ORDER BY sum(pr.cost) DESC
  133.  
  134.  
  135. -------------------------------4--------------------------------------
  136. CREATE TRIGGER Add_visits
  137. ON Visits
  138. INSTEAD OF INSERT AS
  139. BEGIN
  140. IF EXISTS(SELECT *
  141. FROM inserted i
  142. JOIN Visits v ON v.DoctorID = i.DoctorID
  143. WHERE i.VisitingTime BETWEEN v.VisitingTime
  144. AND dateadd(MINUTE, DATEPART(MINUTE, v.Durability), dateadd(HOUR, DATEPART(HOUR, v.Durability),
  145. v.VisitingTime)))
  146. BEGIN
  147. ROLLBACK;
  148. THROW 50001, 'Cannot delete Sotr', 1;
  149. END
  150. ELSE
  151. BEGIN
  152. INSERT INTO Visits (PatientID, DoctorID, VisitingTime, Durability)
  153. SELECT
  154. l.PatientID,
  155. l.DoctorID,
  156. l.VisitingTime,
  157. l.Durability
  158. FROM inserted l
  159. END
  160.  
  161. END
  162.  
  163. DROP TRIGGER Add_visits
  164. INSERT INTO Visits (PatientID, DoctorID, VisitingTime, Durability) VALUES (2, 1, '18-06-12 9:40:00 PM', '00:10')
  165. INSERT INTO Visits (PatientID, DoctorID, VisitingTime, Durability) VALUES (2, 2, '18-06-12 9:40:00 PM', '00:10')
  166. INSERT INTO Visits (PatientID, DoctorID, VisitingTime, Durability) VALUES (2, 5, '18-06-12 11:40:00 PM', '00:10')
  167.  
  168. --------------------------------5-----------------------------------------
  169. ALTER TABLE Doctors
  170. ADD Архив INT
  171.  
  172.  
  173. CREATE TRIGGER DelDoctor
  174. On Doctors
  175. INSTEAD OF DELETE AS
  176. BEGIN
  177. if exists(SELECT * FROM deleted d
  178. JOIN Visits v on v.DoctorID=d.ID
  179. WHERE (SELECT MAX(vt.VisitingTime) FROM Visits vt
  180. WHERE v.DoctorID=d.ID) > GETDATE())
  181. BEGIN
  182. ROLLBACK;
  183. THROW 50001, 'Cannot delete doctor', 1;
  184. END
  185. ELSE
  186. BEGIN
  187. UPDATE Doctors
  188. SET Doctors.Архив=1 WHERE Doctors.ID in (SELECT ID FROM deleted)
  189. END
  190. END
  191.  
  192. DROP TRIGGER DelDoctor
  193. DELETE FROM Doctors WHERE ID=2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement