| 52 | == Да се најде клиент што има направено најголем приход независно од која компанија изнајмувал автомобил |
| 53 | |
| 54 | {{{#!sql |
| 55 | select a.user_id,b.username, a.prihod |
| 56 | from ( |
| 57 | select user_id, |
| 58 | sum(v.daily_rental_price * (c.end_date - c.start_date)) as prihod |
| 59 | from contracts c |
| 60 | join vehicles v on v.vehicle_id = c.vehicle_id |
| 61 | group by c.user_id ) a |
| 62 | join ( |
| 63 | select c.user_id, u.username from clients as c |
| 64 | join users u on u.user_id = c.user_id |
| 65 | ) b on a.user_id = b.user_id |
| 66 | where a.prihod = |
| 67 | (select max(prihod) from (select user_id,sum(v2.daily_rental_price * (c4.end_date - c4.start_date)) as prihod from contracts c4 |
| 68 | join vehicles v2 on v2.vehicle_id = c4.vehicle_id group by user_id) maximprihod) |
| 69 | }}} |
| 70 | |