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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Hopefully an easy SQL code to answer....

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_Number
Process
Date_Time
Weight

Each 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_Number
Process
Opening_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)

Thanks
Garry

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_Number
Process
Date_Time
Weight

Each 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_Number
Process
Opening_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)

Thanks
Garry




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_Weight
FROM
(
SELECT t1.*,t2.PrevWeight,ROW_NUMBER() OVER (PARTITION BY t1.Part_number,t1.Process ORDER BY t1.Date_Time DESC) AS Rn
FROM table t1
OUTER 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
)t
GROUP BY Part_number,Process,PrevWeight


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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
Go to Top of Page

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 tables
t1 stands for actual table
t2 stands for dervied table formed of the OUTER APPLY query

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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?

Go to Top of Page
   

- Advertisement -