SELECT DISTINCT
callerid,
order_id,
max(OrderedDate) as max_OrderedDate,
min(OrderFinished) as min_OrderFinished,
id_client,
order_id,
city
FROM (
SELECT q1.callerid as callerid,
q1.OrderedDate as OrderedDate,
q1.OrderFinished as OrderFinished,
q1.id_client as id_client,
q1.order_id as order_id,
q2.short_name as city
FROM (
SELECT callerid,
OrderedDate,
OrderFinished,
id_client,
main_id_locality,
id as order_id
FROM orders
where source = 'S'
and OrderedDate >= DATE_SUB(CURRENT_DATE, INTERVAL 2 DAY)
) as q1
INNER JOIN (
SELECT id, short_name
FROM DLL2
) as q2
on (q1.main_id) = (
q2.id)) as q1
group by callerid,
order_id, id_client,
order_id,
city
order by OrderedDate desc