37 | | select v.brand , |
38 | | v.chassis_number, |
39 | | c.vehicle_id, |
40 | | m.model_name, |
41 | | m.color,c3.company_name, |
42 | | count(*) as nor from contracts c |
43 | | join vehicles v on v.vehicle_id = c.vehicle_id |
44 | | join models m on v.model_id = m.model_id |
45 | | join companies c3 on c3.company_id = v.company_id |
46 | | where (c.signed_date - interval '2 months')<= c.signed_date and c.signed_date <= now() |
47 | | group by c.vehicle_id , v.brand , v.chassis_number , m.model_name , m.color, c3.company_name |
48 | | having count(*) = (select max(nor) from |
49 | | (select vehicle_id, count(*) as nor from contracts c2 group by vehicle_id) contracts) |
| 37 | select |
| 38 | v.brand , |
| 39 | v.chassis_number, |
| 40 | c.vehicle_id, |
| 41 | m.model_name, |
| 42 | m.color, |
| 43 | c3.company_name, |
| 44 | count(*) as br_iznajmuvanja |
| 45 | from |
| 46 | contracts c |
| 47 | join vehicles v on |
| 48 | v.vehicle_id = c.vehicle_id |
| 49 | join models m on |
| 50 | v.model_id = m.model_id |
| 51 | join companies c3 on |
| 52 | c3.company_id = v.company_id |
| 53 | where |
| 54 | (c.signed_date - interval '2 months')<= c.signed_date |
| 55 | and c.signed_date <= now() |
| 56 | group by |
| 57 | c.vehicle_id , |
| 58 | v.brand , |
| 59 | v.chassis_number , |
| 60 | m.model_name , |
| 61 | m.color, |
| 62 | c3.company_name |
| 63 | having |
| 64 | count(*) = ( |
| 65 | select |
| 66 | max(br_iznajmuvanja) |
| 67 | from |
| 68 | ( |
| 69 | select |
| 70 | vehicle_id, |
| 71 | count(*) as br_iznajmuvanja |
| 72 | from |
| 73 | contracts c2 |
| 74 | group by |
| 75 | vehicle_id) contracts) |