?
Solved

Inventory Stock movement

Posted on 2014-04-30
6
Medium Priority
?
546 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 22

Accepted Solution

by:
Steve Wales earned 1500 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

770 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