1
我正在嘗試創建一個查詢,該查詢在同一個查詢中使用另一個計算的計算字段,但它給了我一個錯誤。在同一查詢中重複使用計算的字段進行新計算
如何以替代方式完成此操作。
查詢:
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'。