I have a table containing fields:
History_ID ORDER_ID Previous_STATUS_ID Next_STATUS_ID
0 2 null 1
1 2 1 2
2 2 2 3
3 1 null 1
4 2 3 4
5 3 null 2
6 2 4 1
7 2 1 2
8 2 2 5
9 7 3 4
10 4 6 2 11 9 3 5
It contains my order ID
and ID
of status. Basically table contains history of STATUS
for every order.
I would like to check if a specific order has a specific order sequence.
Example: if order id 4 had states 2,4,3,1
then return true
Statuses should be one after other so after 2 must be 4, after 4 is 3 and after that 1.
Those sequences give me information (which will be displayed in my report) about flow of specific order.
I need to check for that kind of sequences:
- 1,2,3
- 1,4,3,2
- 4,2
- (1 or 2 or 3 or 4),5,3 - one of those 4 then 5 and then 3
I don't have idea how to start with that kind of query :/
EDIT:
My final report is a table containing information about orders like so:
Orders type Count ...
Profile1 145 ...
Profile2 217 ...
Profile3 12 ...
Profile4 2 ...
I have about 800k+ orders and my report must be done every day, so instead doing report for whole table (checking those STATES and aggregating informations from other tables) I have decided to create a report table:
Order_ID Profile Order_date Customer_profile ...
1 5 '2008-01-03' 2
4 1 '2009-04-10' 3
3 7 '2011-11-03' 1
4523 2 '2012-11-03' 5
So that I can create a night job that will add new orders to this table with my information and I will just do a simple select from that table, to avoid aggregation. First insert will be big, but in next night it will only add new orders.
For example look at order with ID = 2
Next_STATUS_ID ale like so: 1,2,3,4,1,2,5... I want to check if this order has history change like 1,2,5, so my function (select statement) should repeat 1 if it finds those ID's in that order.