Ни в одном из ваших подзапросов нет предложений FROM
, поэтому вы получаете синтаксическую ошибку.Однако на самом деле вам не нужны подзапросы в этих позициях.Для COUNT
вы должны заменить их, например,
SUM(CASE WHEN status=3 THEN 1 ELSE 0 END)
, а для SUM
s вы должны использовать, например,
SUM(CASE WHEN status=3 THEN offer_price ELSE 0 END)
Таким образом, в целом ваш запрос должен быть:
$data = DB::select('
SELECT
DATE(o.created_at) as day,
count(id) as count_all,
SUM(CASE WHEN status=3 THEN 1 ELSE 0 END) as success,
SUM(CASE WHEN status=5 THEN 1 ELSE 0 END) as return_order,
SUM(CASE WHEN status=0 OR status=4 OR status=8 THEN 1 ELSE 0 END) as call_order,
SUM(CASE WHEN status=7 OR status=6 OR status=1 THEN 1 ELSE 0 END) as otkaz,
SUM(CASE WHEN status=2 THEN 1 ELSE 0 END) as nado_dostavit,
SUM(offer_price) as all_price,
SUM(CASE WHEN status=3 THEN offer_price ELSE 0 END) as success_price,
SUM(CASE WHEN status=5 THEN offer_price ELSE 0 END) as return_order_price,
SUM(CASE WHEN status=0 OR status=4 OR status=8 THEN offer_price ELSE 0 END) as call_order_price,
SUM(CASE WHEN status=7 OR status=6 OR status=1 THEN offer_price ELSE 0 END) as otkaz_price,
SUM(CASE WHEN status=2 THEN offer_price ELSE 0 END) as nado_dostavit_price,
(SELECT sum(s.visitors) FROM statistics as s WHERE s.offer_id='.$user_id.') as visitors
FROM
orders as o
WHERE
offer_id='.$user_id.'
AND created_at between "'.$mother_later.'" AND "'.$today.'"
GROUP BY day
ORDER BY day desc
limit 30
');