Problem in calculating closing balance and forward it to opening


I have two tables 1-ledgertable (which contains all the purchasing and issuing details of an inventory) 2- openingstock (contains opening quantity of each item of current session).

I want to create a ledger report using both above given table but I am facing problem to carry forward the balance into opening. Though I tried a lot but it is not giving right result in every row.
I have two tables 1- transactiontable 2- openingstock table
1- transactiontable contains all the details about purchasing item and issuing item in an organization.
2- openingstock contains the opening balance of any particular item of that session

i am giving some values of both the table.
transaction table values are given below:

| trandate |voucherno| itemno| ITEMNAME| recieve| issue| returnback| to_dept| remarks|

|9/16/2017 | 2017916005 |1| Ac Box |NULL| 1| NULL| III DORM | for Commen Room,
|10/7/2017 | 2017107003 |1| Ac Box |NULL|1|NULL|III DORM|Collected by Sanjay|
|9/15/2017|2017000000461|1|Ac Box|1|NULL|NULL|NULL||
|10/6/2017|2017000000526|1|Ac Box|1|NULL|NULL|NULL||
|1/12/2018|20180000000044|1|Ac Box|1|NULL|NULL|NULL||
|5/8/2017|201758002|3|Adopter|NULL|1|NULL|ESTATE MAZAAR GROUND|Collected by Sonu Kumar|
|6/2/2017|201762022|3|Adopter|NULL|14|NULL|ESTATE|Collected by Deepak|
|9/26/2017|2017926001|3|Adopter|NULL|9|NULL|ESTATE|for LED Repair, Collected by Vijay|
|9/26/2017|2017926001|3|Adopter|NULL|7|NULL|ESTATE|for LED Repair, Collected by Vijay|

2- openingstock table values are given below:
itemno itemname opening unit department
3 Adopter 0 NOS QMSTORE
10 Batten holder 37 NOS QMSTORE

I am attaching the format of report which i want.


Please provide:

  • table description in the form of create statements
  • sample data in the form of insert statements
  • output from the sample data you provide
  • the query you're having problems with (and error if you get any)

i am posting the code but it is giving error that "new user can only put 2 links in a post ".. i dont know what is happening here while i am not posting any links.