Solved

Inventory Stock movement

Posted on 2014-04-30
6
497 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

930 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

11 Experts available now in Live!

Get 1:1 Help Now