Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Twi78
Starting Member
3 Posts |
Posted - 2012-03-21 : 10:31:19
|
Hi, I need some help creating a query in SQL.I have a table with the following fields:Event history (primary key numerical value created in process order per part number)Part_NumberProcessDate_TimeWeightEach part number can have multiple processes.I want to be able to create a stock movements table in just one SQL query if possible, whereby I select a start and end date, lets say 01/02/2012 06:00 to 01/03/2012 06:00, and the query would return the following:Part_NumberProcessOpening_Stock_Weight (derived as the last process prior to 01/02/2012 06:00)Process_Weight (derived as the weight associated with each process occuring between 01/02/2012 06:00 and 01/03/2012 06:00)Closing_Stock_Weight (derived as the last process prior to 01/03/2012 06:00)ThanksGarry |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-21 : 10:45:12
|
quote: Originally posted by Twi78 Hi, I need some help creating a query in SQL.I have a table with the following fields:Event history (primary key numerical value created in process order per part number)Part_NumberProcessDate_TimeWeightEach part number can have multiple processes.I want to be able to create a stock movements table in just one SQL query if possible, whereby I select a start and end date, lets say 01/02/2012 06:00 to 01/03/2012 06:00, and the query would return the following:Part_NumberProcessOpening_Stock_Weight (derived as the last process prior to 01/02/2012 06:00)Process_Weight (derived as the weight associated with each process occuring between 01/02/2012 06:00 and 01/03/2012 06:00)Closing_Stock_Weight (derived as the last process prior to 01/03/2012 06:00)ThanksGarry
SELECT Part_number,Process,PrevWeight AS Opening_Stock_Weight,SUM(Weight) AS Process_Weight,MAX(CASE WHEN Rn =1 THEN Weight END) AS Closing_Stock_WeightFROM(SELECT t1.*,t2.PrevWeight,ROW_NUMBER() OVER (PARTITION BY t1.Part_number,t1.Process ORDER BY t1.Date_Time DESC) AS RnFROM table t1OUTER APPLY (SELECT TOP 1 Weight AS PrevWeight FROM table WHERE Part_number = t1.Part_Number AND Date_Time < t1.Date_Time ORDER BY Date_Time DESC)t2)tGROUP BY Part_number,Process,PrevWeight ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Twi78
Starting Member
3 Posts |
Posted - 2012-03-21 : 11:39:52
|
Thanks for the reply.Could you explain the t1 and t2 you have used? t1 refers to the actual table where the data sits?Thanks |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-21 : 12:19:58
|
t1 and t2 are aliases ie short names for tablest1 stands for actual table t2 stands for dervied table formed of the OUTER APPLY query------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Twi78
Starting Member
3 Posts |
Posted - 2012-03-21 : 12:43:03
|
I'm getting a "syntax error (missing operator) in query expression 'MAX(CASE WHEN Rn =1 THEN Weight END)'"Any ideas? |
 |
|
|
|
|
|
|