Solved

Inventory Stock movement

Posted on 2014-04-30
6
491 Views
Last Modified: 2014-04-30
Dear Experts,

In my inventory, I have receiving, withdrawal and adjusting entry as shown in the attachment. What I want is to combine them that shows their movements for easier reconciliation.  

Any help to make this happen would be greatly appreciated.

Thanks.
StockMovement.docx
0
Comment
Question by:JimiJ13
  • 3
  • 3
6 Comments
 
LVL 22

Accepted Solution

by:
Steve Wales earned 500 total points
ID: 40032042
First, I created tables to contain your data (when giving sample tables etc for questions like this, it really does help if you provide the DDL for your test data):

(I apologize for using character data types for dates for the purists, it was quicker and easier to do this with CHAR :))

use demo
go

create table receiving (control char(10), stock_code char(9), action_date char(8), qty int, price int);
create table withdrawl (control char(10), stock_code char(9), action_date char(8), qty int, price int);
create table adjustment (control char(10), stock_code char(9), action_date char(8), qty int, price int);

insert into receiving values ('RR 001','1001','20140301',5,10);
insert into receiving values ('RR 002','1001','20140403',5,9);
insert into withdrawl values ('WIR 001','1001','20140305',2,10);
insert into adjustment values ('IA 001','1001','20140331',-1,10);

Open in new window


Next, since you're unioning three tables to get your data, it was easier to read the final solution by creating a view for the union:

create view all_txns as 
(select  control, stock_code, action_date, qty, price from receiving
union
select  control, stock_code, action_date, qty*-1, price from withdrawl
union
select  control, stock_code, action_date, qty, price from adjustment
)

Open in new window


Then ran the following query:

select a.control, a.stock_code, a.action_Date, a.qty, 
       a.qty*a.price as value, sum(b.qty) as run_qty, sum(b.qty*b.price) as run_val,
       a.price
from all_txns a inner join all_txns b
on (b.action_date <= a.action_date)
group by a.control, a.stock_code, a.action_Date, a.qty, a.qty*a.price, a.price
order by stock_code, action_date

Open in new window


To generate this output:

control    stock_code action_Date qty         value       run_qty     run_val     price
---------- ---------- ----------- ----------- ----------- ----------- ----------- -----------
RR 001     1001       20140301    5           50          5           50          10
WIR 001    1001       20140305    -2          -20         3           30          10
IA 001     1001       20140331    -1          -10         2           20          10
RR 002     1001       20140403    5           45          7           65          9

(4 row(s) affected)

Open in new window


That get you what you want ?

(As an aside, since I did this in Oracle first and then realized it was a SQL Server question, this is much easier to do in Oracle):

select control, stock_code, action_Date, qty, price, price*qty as value,
       sum(qty) over (partition by stock_Code order by action_date) run_qty,
       sum(price*qty) over (partition by stock_Code order by action_date) run_val
from
(
select  control, stock_code, action_date, qty, price from receiving
union
select  control, stock_code, action_date, qty*-1, price from withdrawl
union
select  control, stock_code, action_date, qty, price from adjustment
)
/
0
 

Author Comment

by:JimiJ13
ID: 40033052
Steve,

Your great effort is truly appreciated, and I think few more tweaks are needed to get my desired output.

When I tried the last query, I got an error:

Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'order'.

select control, stock_code, action_Date, qty, price, price*qty as value,
       sum(qty) over (partition by stock_Code order by action_date) run_qty,
       sum(price*qty) over (partition by stock_Code order by action_date) run_val
from
(
select  control, stock_code, action_date, qty, price from receiving
union
select  control, stock_code, action_date, qty*-1, price from withdrawl
union
select  control, stock_code, action_date, qty, price from adjustment
)

Open in new window

0
 
LVL 22

Expert Comment

by:Steve Wales
ID: 40033073
The last query won't work on SQL Server.  It was an after thought - that's how you do it in Oracle:

"As an aside, since I did this in Oracle first and then realized it was a SQL Server question, this is much easier to do in Oracle"

I did it in Oracle, then realized it was a SQL Server question so had to redo it in SQL Server, but included the Oracle query for (a) sake of comparison and (b) so I could find it easily again later :)
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:JimiJ13
ID: 40033138
Sorry! I thought the last one was a way of doing it in MS SQL.


Thanks.
0
 

Author Closing Comment

by:JimiJ13
ID: 40033526
It did not give me the complete solution (no solution given for MS SQL) but a good lead to coming up with the final via other resources or means.

Please refer to http://www.experts-exchange.com/Database/MS-SQL-Server/Q_28423550.html#a40033357

Thanks.
0
 
LVL 22

Expert Comment

by:Steve Wales
ID: 40033529
Actually, if you recheck the post - the top 90% of the post is the full solution in SQL Server.

I'm sorry if my comments about Oracle at the very bottom clouded the issue for you.
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now