/* Affected rows: 0 Найденные строки: 15 834 Предупреждения: 0 Длительность 1 query: 0,359 sec. (+ 50,591 sec. network)*/
SELECT s.thing_id as start_tid, s.thing_status as start_ts, s.thing_price as start_tp, s.thing_store_num as start_tsnum,d.date_st as start_date
FROM `states` AS `s`
JOIN (
SELECT `thing_id`, MAX(`state_change_date`) AS `date_st`
FROM `states`
WHERE `state_change_date` <= "2015-01-01"
GROUP BY `thing_id`
) AS `d` ON `d`.`thing_id` = `s`.`thing_id` AND `d`.`date_st` = `s`.`state_change_date`
right join
(SELECT sf.thing_id as finish_tid, sf.thing_status as finish_ts, sf.thing_price as finish_tp, sf.thing_store_num as finish_tsnum,df.date_f as finish_date
FROM `states` AS `sf`
JOIN (
SELECT `thing_id`, MAX(`state_change_date`) AS `date_f`
FROM `states`
WHERE `state_change_date` <= "2015-01-01"
GROUP BY `thing_id`
) AS `df` ON `df`.`thing_id` = `sf`.`thing_id` AND `df`.`date_f` = `sf`.`state_change_date`
) as `ft` on `s`.`thing_id`=`ft`.`finish_id`
QR.SQL.Add('select n.node_name, '+
'count(case when (date(s.shop_date)<"'+date2mysql(report_datestart.Text)+'" and status=2) or (date(s.shop_date)<"'+date2mysql(report_datestart.Text)+'" and status=4 and date(sell_date)>="'+date2mysql(report_datestart.Text)+'") then s.id end) as tot_cnt,'+
'round(sum(case when date(s.shop_date)<"'+date2mysql(report_datestart.Text)+'" and (status=2) or (date(s.shop_date)<"'+date2mysql(report_datestart.Text)+'" and status=4 and date(sell_date)>="'+date2mysql(report_datestart.Text)+'") then total_weight end),2) as tot_wt,'+
'sum(case when date(s.shop_date)<"'+date2mysql(report_datestart.Text)+'" and (status=2) or (date(s.shop_date)<"'+date2mysql(report_datestart.Text)+'" and status=4 and date(sell_date)>="'+date2mysql(report_datestart.Text)+'") then kredit end) as tot_kr,'+
'sum(case when date(s.shop_date)<"'+date2mysql(report_datestart.Text)+'" and (status=2) or (date(s.shop_date)<"'+date2mysql(report_datestart.Text)+'" and status=4 and date(sell_date)>="'+date2mysql(report_datestart.Text)+'") then price end) as tot_pr,'+
'count(case when date(s.shop_date) between "'+date2mysql(report_datestart.Text)+'" and "'+date2mysql(report_dateend.Text)+'" and (status in(2,4)) then s.id end) as new_cnt,'+
'round(sum(case when date(s.shop_date) between "'+date2mysql(report_datestart.Text)+'" and "'+date2mysql(report_dateend.Text)+'" and (status in(2,4)) then total_weight end),2) as new_wt,'+
'sum(case when date(s.shop_date) between "'+date2mysql(report_datestart.Text)+'" and "'+date2mysql(report_dateend.Text)+'" and (status in(2,4)) then kredit end) as new_kr,'+
'sum(case when date(s.shop_date) between "'+date2mysql(report_datestart.Text)+'" and "'+date2mysql(report_dateend.Text)+'" and (status in(2,4)) then price end)as new_pr,'+
'count(case when date(s.sell_date) between "'+date2mysql(report_datestart.Text)+'" and "'+date2mysql(report_dateend.Text)+'" and status=4 then s.id end) as sell_cnt,'+
'round(sum(case when date(s.sell_date) between "'+date2mysql(report_datestart.Text)+'" and "'+date2mysql(report_dateend.Text)+'" and status=4 then total_weight end),2) as sell_wt,'+
'sum(case when date(s.sell_date) between "'+date2mysql(report_datestart.Text)+'" and "'+date2mysql(report_dateend.Text)+'" and status=4 then kredit end) as sell_kr,'+
'sum(case when date(s.sell_date) between "'+date2mysql(report_datestart.Text)+'" and "'+date2mysql(report_dateend.Text)+'" and status=4 then vyruchka end) as sell_pr,'+
'sum(case when date(s.sell_date) between "'+date2mysql(report_datestart.Text)+'" and "'+date2mysql(report_dateend.Text)+'" and status=4 and price>vyruchka then price-vyruchka end) as sell_discount,'+
'count(case when store_num is not null and date(s.sell_date) between "'+date2mysql(report_datestart.Text)+'" and "'+date2mysql(report_dateend.Text)+'" and status=4 then s.id end) as sell_l_cnt,'+
'round(sum(case when store_num is not null and date(s.sell_date) between "'+date2mysql(report_datestart.Text)+'" and "'+date2mysql(report_dateend.Text)+'" and status=4 then total_weight end),2) as sell_l_wt,'+
'sum(case when store_num is not null and date(s.sell_date) between "'+date2mysql(report_datestart.Text)+'" and "'+date2mysql(report_dateend.Text)+'" and status=4 then kredit end) as sell_l_kr,'+
'sum(case when store_num is not null and date(s.sell_date) between "'+date2mysql(report_datestart.Text)+'" and "'+date2mysql(report_dateend.Text)+'" and status=4 then vyruchka end) as sell_l_pr,'+
'count(case when date(s.sell_date) between "'+date2mysql(report_datestart.Text)+'" and "'+date2mysql(report_dateend.Text)+'" and status=5 then s.id end) as ret_cnt,'+
'round(sum(case when date(s.sell_date) between "'+date2mysql(report_datestart.Text)+'" and "'+date2mysql(report_dateend.Text)+'" and status=5 then total_weight end),2) as ret_wt,'+
'sum(case when date(s.sell_date) between "'+date2mysql(report_datestart.Text)+'" and "'+date2mysql(report_dateend.Text)+'" and status=5 then kredit end) as ret_kr,'+
'sum(case when date(s.sell_date) between "'+date2mysql(report_datestart.Text)+'" and "'+date2mysql(report_dateend.Text)+'" and status=5 then price end) as ret_pr,'+
'count(case when (status=2 and date(s.shop_date)<="'+date2mysql(report_dateend.Text)+'") or (date(s.shop_date)<="'+date2mysql(report_dateend.Text)+'" and status=4 and date(sell_date)>"'+date2mysql(report_dateend.Text)+'") then s.id end) as end_cnt,'+
'round(sum(case when (status=2 and date(s.shop_date)<="'+date2mysql(report_dateend.Text)+'") or (date(s.shop_date)<="'+date2mysql(report_dateend.Text)+'" and status=4 and date(sell_date)>"'+date2mysql(report_dateend.Text)+'") then total_weight end),2) as end_wt,'+
'sum(case when (status=2 and date(s.shop_date)<="'+date2mysql(report_dateend.Text)+'") or (date(s.shop_date)<="'+date2mysql(report_dateend.Text)+'" and status=4 and date(sell_date)>"'+date2mysql(report_dateend.Text)+'") then kredit end) as end_kr,'+
'sum(case when (status=2 and date(s.shop_date)<="'+date2mysql(report_dateend.Text)+'") or (date(s.shop_date)<="'+date2mysql(report_dateend.Text)+'" and status=4 and date(sell_date)>"'+date2mysql(report_dateend.Text)+'") then s.price end) as end_pr '+
'from nodes n left join storage s on n.Id=s.node_num group by s.node_num order by node_name'
);