https://stackoverflow.com/questions/2330840/mysql-invalid-use-of-group-function
出錯SQL:
錯誤原因:
1. 不能在?WHERE
?子句中使用聚合(或分組)函數
2.?HAVING
?只能篩選分組后的聚合結果或分組字段
# Write your MySQL query statement below
select round(sum(IF(customer_pref_delivery_date = order_date, 1, 0))/count(*), 2) as immediate_percentage
from Delivery
group by customer_id
HAVING (order_date = MIN(order_date))
修改:
SELECTROUND(SUM(IF(first_order_date = first_pref_date, 1, 0)) / COUNT(*) * 100, 2) AS immediate_percentage
FROM (SELECT customer_id,MIN(order_date) AS first_order_date,MIN(customer_pref_delivery_date) AS first_pref_dateFROM DeliveryGROUP BY customer_id
) AS first_orders;