我正在尝试创建一个查询,该查询将计算字段用于同一查询中的另一个计算,但它给了我一个错误。
如何以替代方式完成此操作。
询问 :
SELECT MIN(q.order_created) AS first_ordered,
MAX(q.order_created) AS last_ordered,
SUM(IF(DATE_ADD(q.order_created,INTERVAL 12 MONTH) >= NOW(),pq.product_qty,0))/12 AS monthly_rate,
SUM(pq.product_qty) AS yearly_sales,
SUM((pq.product_cost_price * pq.product_qty) - pq.product_total_price ) AS **net_sold**,
SUM(pq.product_cost_price * pq.product_qty) AS **total_ordered**,
**100 - ((total_ordered - net_sold)/total_ordered )*100 AS discount**,
q.billing_account_id AS custid
FROM quotes q
LEFT JOIN products_quotes pq
ON q.id = pq.quote_id
WHERE pq.product_id = '28e96e3d-460f-49fc-7d52-4f390b86d6b8'
AND q.deleted = 0
AND pq.deleted = 0
GROUP BY q.billing_account_id
ORDER BY q.order_created
GROUP BY q.billing_account_id
ORDER BY q.order_created
给出错误:“字段列表”中的未知列“total_ordered”。