I know, this question has been asked very often but I know my error, I know how I could fix it, but I canĀ“t find the point where the error is. In my opinion, all the subqueries have different and unique names, I even gave the columns different names then the subqueries. Any help would be appreciated. Where is the point I am missing an alias?
Whenever I am trying to run this query I get the response “Every derived table must have its alias”, which is an understandable error message, but I can’t figure out where my error is located.
SELECT mso.entity_id, GROUP_CONCAT(msh.comment) AS comment, msoa.lastname, base_grand_total, mso.created_at, mso.status, marketplace_order_id AS amazon_order_id, clvData.recurrenceRate, clvData.avgRepRate FROM mag_sales_flat_order AS mso LEFT JOIN mag_sales_flat_order_status_history AS msh ON mso.entity_id = msh.parent_id LEFT JOIN mag_sales_flat_order_address AS msoa ON mso.entity_id = msoa.parent_id left join ( select cast(((cet.cec - cnt.cnc) / cst.csc) AS decimal(6, 2)) as recurrenceRate, avg(repRate.countedOrders) AS avgRepRate from( Select *, ( select count(customer_email) AS csc from mag_sales_flat_order where created_at between '2017-01-01' and '2017-12-31' ) AS cst, ( select count(customer_email) AS cec from mag_sales_flat_order where created_at between '2017-01-01' and '2020-12-31' ) AS cet, ( select count(mso_new.customer_email) AS cnc from ( select * from mag_sales_flat_order where created_at between '2018-01-01' and current_date() ) AS mso_new left join ( select * from mag_sales_flat_order where created_at between '2017-01-01' and '2017-12-31' ) AS mso_old on mso_new.customer_email = mso_old.customer_email )) AS cnt join ( select customer_email, count(grand_total) as countedOrders, sum(grand_total) as summedOrders from mag_sales_flat_order group by customer_email ) AS repRate on cl.customer_email = repRate.customer_email ) AS clvData on mso.customer_email = clvData.customer_email WHERE store_id IN({$store['id']}) AND ( mso.status = 'complete' OR mso.status = 'closed' OR mso.status = 'processing' OR mso.status = 'exported' OR mso.status LIKE 'pending%' ) AND ( DATE_FORMAT(mso.created_at, '%Y-%m-%d') >= '$begin_date' ) AND ( DATE_FORMAT(mso.created_at, '%Y-%m-%d') <= '$end_date' ) GROUP BY entity_id;
Advertisement
Answer
SELECT mso.entity_id, GROUP_CONCAT(msh.comment) AS comment, msoa.lastname, base_grand_total, mso.created_at, mso.status, marketplace_order_id AS amazon_order_id, clvData.recurrenceRate, clvData.avgRepRate FROM mag_sales_flat_order AS mso LEFT JOIN mag_sales_flat_order_status_history AS msh ON mso.entity_id = msh.parent_id LEFT JOIN mag_sales_flat_order_address AS msoa ON mso.entity_id = msoa.parent_id LEFT JOIN ( SELECT cast(((cet.cec - cnt.cnc) / cst.csc) AS decimal(6, 2)) as recurrenceRate, avg(repRate.countedOrders) AS avgRepRate FROM ( SELECT *, ( SELECT count(customer_email) AS csc FROM mag_sales_flat_order WHERE created_at BETWEEN '2017-01-01' AND '2017-12-31' ) AS cst, ( SELECT count(customer_email) AS cec FROM mag_sales_flat_order WHERE created_at BETWEEN '2017-01-01' AND '2020-12-31' ) AS cet, ( SELECT count(mso_new.customer_email) AS cnc FROM ( SELECT * FROM mag_sales_flat_order WHERE created_at BETWEEN '2018-01-01' AND getdate() ) AS mso_new LEFT JOIN ( SELECT * FROM mag_sales_flat_order WHERE created_at BETWEEN '2017-01-01' AND '2017-12-31' ) AS mso_old on mso_new.customer_email = mso_old.customer_email ) AS cnt ) as cl JOIN ( SELECT customer_email, count(grand_total) as countedOrders, sum(grand_total) as summedOrders FROM mag_sales_flat_order GROUP BY customer_email ) AS repRate on cl.customer_email = repRate.customer_email ) AS clvData on mso.customer_email = clvData.customer_email WHERE store_id IN({ $ store ['id'] }) AND ( mso.status = 'complete' OR mso.status = 'closed' OR mso.status = 'processing' OR mso.status = 'exported' OR mso.status LIKE 'pending%' ) AND ( DATE_FORMAT(mso.created_at, '%Y-%m-%d') >= '$begin_date' ) AND ( DATE_FORMAT(mso.created_at, '%Y-%m-%d') <= '$end_date' ) GROUP BY entity_id;