218 | | == Kолку пари се трошат за платите на вработените во секој ветеринарен центар. |
219 | | {{{ |
220 | | SELECT |
221 | | vc.name AS vet_center_name, |
222 | | SUM(CASE WHEN j.predictedSalery LIKE '%EUR%' THEN CAST(SUBSTRING(j.predictedSalery, 1, POSITION(' ' IN j.predictedSalery) - 1) AS DECIMAL) ELSE 0 END) AS total_salary_expense |
223 | | FROM |
224 | | project.vet_centers vc |
225 | | JOIN |
226 | | project.jobs j ON vc.id = j.vetCentersID |
227 | | LEFT JOIN |
228 | | project.users u ON vc.id = u.jobs_id |
229 | | GROUP BY |
230 | | vc.id, vet_center_name |
231 | | ORDER BY |
232 | | total_salary_expense DESC; |
| 218 | == За секој ветеринарен центар, колку пари одвојува за нови вработени и која е просечната плата по вработен која се нуди. |
| 219 | {{{ |
| 220 | WITH SalaryData AS ( |
| 221 | SELECT |
| 222 | v.id AS vet_center_id, |
| 223 | v.name AS vet_center_name, |
| 224 | j.predictedsalery AS salary |
| 225 | FROM |
| 226 | vet_centers v |
| 227 | JOIN |
| 228 | jobs j ON v.id = j.vetCentersID |
| 229 | ), |
| 230 | SalaryAggregates AS ( |
| 231 | SELECT |
| 232 | vet_center_id, |
| 233 | vet_center_name, |
| 234 | COUNT(salary) AS number_of_employees, |
| 235 | SUM(CAST(REPLACE(salary, ' EUR', '') AS DECIMAL)) AS total_salary_expenses, |
| 236 | AVG(CAST(REPLACE(salary, ' EUR', '') AS DECIMAL)) AS average_salary |
| 237 | FROM |
| 238 | SalaryData |
| 239 | GROUP BY |
| 240 | vet_center_id, vet_center_name |
| 241 | ) |
| 242 | |
| 243 | SELECT |
| 244 | sa.vet_center_name, |
| 245 | sa.number_of_employees, |
| 246 | sa.total_salary_expenses, |
| 247 | sa.average_salary |
| 248 | FROM |
| 249 | SalaryAggregates sa |
| 250 | ORDER BY |
| 251 | sa.total_salary_expenses DESC; |
326 | | == Прикажи го продуктот за кој постојат најмал број направени нарачки. |
327 | | {{{ |
328 | | |
329 | | WITH product_orders_count AS ( |
330 | | SELECT |
331 | | p.id AS product_id, |
332 | | p.name AS product_name, |
333 | | p.description AS product_description, |
334 | | p.price AS product_price, |
335 | | COALESCE(COUNT(o.id), 0) AS orders_count |
336 | | FROM |
337 | | project.products p |
338 | | LEFT JOIN |
339 | | project.product_are_made_orders pamo ON p.id = pamo.id_products |
340 | | LEFT JOIN |
341 | | project.orders o ON pamo.id_orders = o.id |
342 | | GROUP BY |
343 | | p.id, p.name, p.description, p.price |
344 | | ) |
345 | | SELECT |
346 | | product_id, |
347 | | product_name, |
348 | | product_description, |
349 | | product_price, |
350 | | orders_count |
351 | | FROM |
352 | | product_orders_count |
353 | | WHERE |
354 | | orders_count = ( |
355 | | SELECT MIN(orders_count) |
356 | | FROM product_orders_count |
357 | | ); |
358 | | |
359 | | }}} |