| 2 | == Прикажување на вработен (доктор) кој што има напишено најмногу извештаи за едно исто милениче. |
| 3 | За да го тестираме овој прашалник ги додадовме следниве податоци: |
| 4 | {{{ |
| 5 | INSERT INTO project.reports |
| 6 | (description, usersid, petsid) |
| 7 | VALUES('Za isto zivotno', 8, 1); |
| 8 | }}} |
| 9 | {{{ |
| 10 | WITH DoctorPetReportsCount AS ( |
| 11 | SELECT |
| 12 | u.id AS doctor_id, |
| 13 | u.name AS doctor_name, |
| 14 | r.petsID AS pet_id, |
| 15 | COUNT(r.id) AS reports_count |
| 16 | FROM |
| 17 | project.reports r |
| 18 | LEFT JOIN |
| 19 | project.users u ON r.usersID = u.id |
| 20 | GROUP BY |
| 21 | u.id, u.name, r.petsID |
| 22 | ), |
| 23 | |
| 24 | MaxReportsPerPet AS ( |
| 25 | SELECT |
| 26 | pet_id, |
| 27 | MAX(reports_count) AS max_reports_count |
| 28 | FROM |
| 29 | DoctorPetReportsCount |
| 30 | GROUP BY |
| 31 | pet_id |
| 32 | ), |
| 33 | |
| 34 | PetsWithMaxReports AS ( |
| 35 | SELECT |
| 36 | pet_id, |
| 37 | max_reports_count |
| 38 | FROM |
| 39 | MaxReportsPerPet |
| 40 | WHERE |
| 41 | max_reports_count = (SELECT MAX(max_reports_count) FROM MaxReportsPerPet) |
| 42 | ) |
| 43 | |
| 44 | SELECT |
| 45 | dprc.doctor_id, |
| 46 | dprc.doctor_name, |
| 47 | dprc.pet_id, |
| 48 | dprc.reports_count |
| 49 | FROM |
| 50 | DoctorPetReportsCount dprc |
| 51 | JOIN |
| 52 | PetsWithMaxReports pwmr ON dprc.pet_id = pwmr.pet_id AND dprc.reports_count = pwmr.max_reports_count |
| 53 | JOIN |
| 54 | project.users u ON dprc.doctor_id = u.id; |
| 55 | |
| 56 | }}} |
26 | | == Прикажување на вработен (доктор) кој што има напишено најмногу извештаи за едно исто милениче. |
27 | | За да го тестираме овој прашалник ги додадовме следниве податоци: |
28 | | {{{ |
29 | | INSERT INTO project.reports |
30 | | (description, usersid, petsid) |
31 | | VALUES('Za isto zivotno', 8, 1); |
32 | | }}} |
33 | | {{{ |
34 | | WITH DoctorPetReportsCount AS ( |
35 | | SELECT |
36 | | u.id AS doctor_id, |
37 | | u.name AS doctor_name, |
38 | | r.petsID AS pet_id, |
39 | | COUNT(r.id) AS reports_count |
40 | | FROM |
41 | | project.reports r |
42 | | LEFT JOIN |
43 | | project.users u ON r.usersID = u.id |
44 | | GROUP BY |
45 | | u.id, u.name, r.petsID |
46 | | ), |
47 | | |
48 | | MaxReportsPerPet AS ( |
49 | | SELECT |
50 | | pet_id, |
51 | | MAX(reports_count) AS max_reports_count |
52 | | FROM |
53 | | DoctorPetReportsCount |
54 | | GROUP BY |
55 | | pet_id |
56 | | ), |
57 | | |
58 | | PetsWithMaxReports AS ( |
59 | | SELECT |
60 | | pet_id, |
61 | | max_reports_count |
62 | | FROM |
63 | | MaxReportsPerPet |
64 | | WHERE |
65 | | max_reports_count = (SELECT MAX(max_reports_count) FROM MaxReportsPerPet) |
66 | | ) |
67 | | |
68 | | SELECT |
69 | | dprc.doctor_id, |
70 | | dprc.doctor_name, |
71 | | dprc.pet_id, |
72 | | dprc.reports_count |
73 | | FROM |
74 | | DoctorPetReportsCount dprc |
75 | | JOIN |
76 | | PetsWithMaxReports pwmr ON dprc.pet_id = pwmr.pet_id AND dprc.reports_count = pwmr.max_reports_count |
77 | | JOIN |
78 | | project.users u ON dprc.doctor_id = u.id; |
79 | | |
80 | | }}} |