Reputation: 57
I have to create a MySQL Query for a report.
I have a table history
which keeps record of status changes of a product's order. I have these statuses for an order's life cycle (the flow of an order): new, confirmed, processing, shipped, delivered, cancelled, returned. An order doesn't necessarily follow this status flow, for example: A new order can be cancelled immediately, or it can be cancelled while in processing. This makes the accurate reporting more complex.
Now I want to make a Supply Chain report of orders which were confirmed, processed, shipped and then delivered. But this report should exclude orders which were cancelled at any stage or returned.
Consider this example:
An order (id: 34) was placed by a customer. Initial status: New. Confirmed by an Agent, status set to Confirmed. Supply Chain manager verifies availability, status changes to Processing, then shipped and then delivered.
Another order (id: 55) comes, New, confirmed, processing but cancelled. history
looks like:
id order_id status
111 34 new
112 34 confirmed
113 55 new
114 55 confirmed
115 55 processing
116 34 processing
117 55 cancelled
118 34 shipped
119 34 delivered
How do I make a report query to get only those orders which were
How can I write a query to get these kinds of reports?
Upvotes: 0
Views: 399
Reputation: 862
for new order:select distinct(order_id) from history where status = new for new processed:select distinct(order_id) from history where status = processed and so on
for those order which are new, processed and delivered
select t1.order_id from history as t1, history as t2, history as t3 where t1.order_id=t2.order_id and t2.order_id=t3.order_id and t1.status=new and t2.status = processed and t3.status = delivered
like this go on for others
Upvotes: 1