日付と最後の注文日までに顧客が行った注文の総数を見つけたいと思っていました。
お客様
custome_id customer_name
1 JOHN
2 ALEX
注文
order_id customer_id order_date status
R1 1 06/06/2013 completed
R2 1 05/29/2013 completed
B1091 1 01/17/2011 canceled
B2192 1 12/24/2010 completed
注: order_id は増分ではないため、最後の注文を見つけるのに役立ちません。
私が試しているクエリは
select customer.customer_id, customer.customer_name, order.order_id as last_order_id, max(order.order_date) as maxOrderDate,
sum( case when order.status='completed' then 1 else 0) as completed_orders,
count( order_id) as total_orders
from customer as customer inner join order as order
on customer.customer_id = order.customer_id
where customer.id = 1
group by customer.customer_id, customer.customer_name, order.order_id
期待される結果として
customer_id customer_name Last_order_id maxOrderDate completed_orders total_orders
1 JOHN R1 06/06/2013 3 4