| Author |
Topic |
|
priestlm
Starting Member
13 Posts |
Posted - 2010-01-04 : 10:32:43
|
Hey guys, wonder if you can help with the following - ive spent 4 days on this and keep getting the wrong results....I need to return data from the orders current step to a specified step description for the product in question... lets say i want to return everything up to & including "Pack"Im using:Select Orders.Order, ProcessSteps.StepDescriptionFrom Orders Full Join ProcessSteps.Product On Orders.Product like ProcessSteps.ProductWhere ProcessSteps.Step >= Order.CurrentStep and ProcessSteps.Step <= (Select Top 1 Min(ProcessSteps) As Step From Orders Inner Join ProcessSteps on Orders.Product like ProcessSteps.Product Where ProcessSteps.StepDescription like 'Pack' and ProcessSteps.Step >= Orders.CurrentStep Group by Orders.Order)Order By Orders.Order The result set i want to return is:001 Weld001 Pack002 Start002 Trace002 PackBut its returning001 Weld001 Pack001 End002 Start002 Trace002 Pack002 Weld002 EndIt doesnt seem to be working with the subquery, although if i run the subquery separatly, it returns which step pack is for each order...Any help you can offer will be greatfully received....Table 1 - OrdersOrder Product CurrentStep001 A 2002 B 1Table 2 - ProcessStepsProduct Step StepDescriptionA 1 StartA 2 Weld A 3 PackA 4 EndB 1 StartB 2 TraceB 3 PackB 4 WeldB 5 End |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-01-04 : 11:35:38
|
This?Sample datadeclare @Orders table (Orderno int, Product varchar(1), CurrentStep int)insert @Ordersselect 001, 'A', 2union all select 002, 'B', 1declare @ProcessSteps table(Product varchar(1), Step int, StepDescription varchar(10))insert @ProcessStepsselect 'A', 1, 'Start'union all select 'A', 2, 'Weld'union all select 'A', 3, 'test' union all select 'A', 4, 'Pack'union all select 'A', 5, 'End'union all select 'B', 1, 'Start'union all select 'B', 2, 'Trace'union all select 'B', 3, 'Pack'union all select 'B', 4, 'Weld'union all select 'B', 5, 'End' Queryselect t.Orderno,t.StepDescription from (select a.Orderno,a.Product,b.StepDescription,b.Stepfrom @Orders aleft join @ProcessSteps bon a.Product = b.Product) tinner join(select Product,Step,StepDescription from @ProcessSteps where StepDescription like 'Pack%') t1on t.Product = t1.Product and t.Step <= t1.Step |
 |
|
|
balaganapathy.n
Starting Member
18 Posts |
Posted - 2010-01-05 : 01:46:31
|
| A slightly modified version of the above query, if you like to try.declare @Orders table (Orderno int, Product varchar(1), CurrentStep int)insert @Orders select 1, 'A', 2 union all select 2, 'B', 1declare @ProcessSteps table (Product varchar(1), Step int, StepDescription varchar(10))insert @ProcessStepsselect 'A', 1, 'Start'union all select 'A', 2, 'Weld'union all select 'A', 3, 'test' union all select 'A', 4, 'Pack'union all select 'A', 5, 'End'union all select 'B', 1, 'Start'union all select 'B', 2, 'Trace'union all select 'B', 3, 'Pack'union all select 'B', 4, 'Weld'union all select 'B', 5, 'End'SELECT A.Orderno, B.StepDescription FROM @Orders AS AINNER JOIN (SELECT * FROM @ProcessSteps O WHERE EXISTS(SELECT 1 FROM @ProcessSteps I WHERE O.Product = I.Product AND O.Step <= I.Step AND I.StepDescription = 'Pack') ) AS B ON A.Product = B.Productbalaganapathy n. |
 |
|
|
priestlm
Starting Member
13 Posts |
Posted - 2010-01-05 : 10:36:22
|
Thanks for the help guys, both sets of code are great... ive used the first sample from balaganapathy.n & there is a slight problem ...If my Processsteps have multiple steps of the same description the code is returning the all process steps to the last instance, i need the code to return the process step to the the first instance, have you any ideas how to do that?Thanks alot for you helpdeclare @Orders table (Orderno int, Product varchar(1), CurrentStep int)insert @Orders select 1, 'A', 2 union all select 2, 'B', 1 union all select 3, 'A', 1declare @ProcessSteps table (Product varchar(1), Step int, StepDescription varchar(10))insert @ProcessStepsselect 'A', 1, 'Start'union all select 'A', 1, 'Start'union all select 'A', 2, 'Pack'union all select 'A', 3, 'Weld'union all select 'A', 4, 'Pack'union all select 'A', 5, 'test' union all select 'A', 6, 'Pack'union all select 'A', 7, 'End'union all select 'B', 1, 'Start'union all select 'B', 2, 'Pack'union all select 'B', 3, 'Trace'union all select 'B', 4, 'Pack'union all select 'B', 5, 'Weld'union all select 'B', 6, 'Pack'union all select 'B', 7, 'End'SELECT A.Orderno, B.StepDescription FROM @Orders AS AINNER JOIN (SELECT * FROM @ProcessSteps O WHERE EXISTS(SELECT 1 FROM @ProcessSteps I, @Orders ZWHERE O.Product = I.Product AND O.Step > Z.CurrentStep + 1AND O.Step <= I.Step AND I.StepDescription like 'Pack')) AS BON A.Product = B.ProductOrder by Orderno, step |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-01-05 : 11:06:38
|
| What is your expected output for this new set of sample data? |
 |
|
|
priestlm
Starting Member
13 Posts |
Posted - 2010-01-05 : 11:29:26
|
| At the moment the code returns:1 Weld1 Pack1 test1 Pack2 Trace2 Pack2 Weld2 Pack3 Weld3 Pack3 test3 PackBut i want see1 Weld1 Pack2 Trace2 Pack3 Weld3 PackSo the code only returns the data up until the first instance of 'Pack' for the given orderThanksMark |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-06 : 03:54:20
|
| can you explain how order 2 started with Trace?since current step is 1 shouldnt it start with Pack? |
 |
|
|
priestlm
Starting Member
13 Posts |
Posted - 2010-01-06 : 04:00:04
|
| Sorry - yes you are right, the output i want to see is:1 Pack2 Start2 Pack3 Start3 Pack |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-06 : 04:09:23
|
| [code]SELECT o.Orderno,sd.StepDescriptionFROM Orders oCROSS APPLY (SELECT TOP 1 Step FROM ProcessSteps WHERE Product=o.Product AND Step>=o.CurrentStep AND StepDescription='Pack' ORDER BY Step) tCROSS APPLY (SELECT StepDescription FROM ProcessSteps WHERE Product=o.Product AND Step>=o.CurrentStep AND Step <= t.Step ) sd[/code] |
 |
|
|
balaganapathy.n
Starting Member
18 Posts |
Posted - 2010-01-06 : 05:18:12
|
| Here find the modified script, check and let me know.declare @Orders table (Orderno int, Product varchar(1), CurrentStep int)insert @Orders select 1, 'A', 2 union all select 2, 'B', 1declare @ProcessSteps table (Product varchar(1), Step int, StepDescription varchar(10))insert @ProcessStepsselect 'A', 1, 'Start'union all select 'A', 2, 'Weld'union all select 'A', 3, 'test' union all select 'A', 4, 'Pack'union all select 'A', 5, 'End'union all select 'A', 6, 'Pack'union all select 'A', 7, 'End2'union all select 'B', 1, 'Start'union all select 'B', 2, 'Trace'union all select 'B', 3, 'Pack'union all select 'B', 4, 'Weld'union all select 'B', 5, 'End'SELECT A.Orderno, B.StepDescription FROM @Orders AS AINNER JOIN ((SELECT O.* FROM @ProcessSteps O INNER JOIN (SELECT Product, MIN(Step) AS Step, StepDescription FROM @ProcessSteps WHERE StepDescription = 'Pack' GROUP BY Product, StepDescription) I ON O.Product = I.Product AND O.Step <= I.Step)) AS BON A.Product = B.ProductAND A.CurrentStep <= B.Step |
 |
|
|
|
|
|