255 | | == Прикажување која терапија за која дијагноза е препишена и од кој доктор е препишена. |
256 | | {{{ |
257 | | SELECT |
258 | | t.description AS therapy_description, |
259 | | d.description AS diagnosis_description, |
260 | | u.name AS doctor_name, |
261 | | u.lastname AS doctor_lastname |
262 | | FROM |
263 | | project.therapy t |
264 | | JOIN |
265 | | project.diagnostics d ON d.id = t.diagnosticsID |
266 | | JOIN |
267 | | project.users u ON u.id = d.usersID |
268 | | JOIN |
269 | | project.pets p ON p.id = d.usersID |
270 | | ORDER BY |
271 | | t.id; |
| 255 | == За секој продукт колку нарачки за него се направени и од кои корисници се направени. |
| 256 | {{{ |
| 257 | WITH ProductOrders AS ( |
| 258 | SELECT |
| 259 | p.id AS product_id, |
| 260 | p.name AS product_name, |
| 261 | o.id AS order_id, |
| 262 | o.quantity AS order_quantity, |
| 263 | u.id AS user_id, |
| 264 | u.name AS user_name, |
| 265 | u.lastname AS user_lastname |
| 266 | FROM |
| 267 | project.products p |
| 268 | JOIN |
| 269 | project.product_are_made_orders pmo ON p.id = pmo.id_products |
| 270 | JOIN |
| 271 | project.orders o ON pmo.id_orders = o.id |
| 272 | JOIN |
| 273 | project.users u ON o.userid = u.id |
| 274 | ) |
| 275 | SELECT |
| 276 | po.product_id, |
| 277 | po.product_name, |
| 278 | COUNT(po.order_id) AS total_orders, |
| 279 | STRING_AGG(DISTINCT CONCAT(po.user_name, ' ', po.user_lastname), ', ') AS users |
| 280 | FROM |
| 281 | ProductOrders po |
| 282 | GROUP BY |
| 283 | po.product_id, po.product_name |
| 284 | ORDER BY |
| 285 | po.product_name; |
| 286 | |