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 2000 Forums
 Transact-SQL (2000)
 Query help

Author  Topic 

bettyatolive
Starting Member

21 Posts

Posted - 2006-06-08 : 05:03:14
I am working on Stocks. and i have 3 tables:stock,stockIn,stockOut.
Stosk stores the items. StockIn stores the coming or received stock,
StockOut stores the outgoing sales.
The table structures are as follows:

Stock(Id,name,description,unitprice,stockInHand,stockSold)
StockIn(Id_fk,received,date,details,receiptno)
StockOut(Id_fk,sold,date,receiptno,salesamount,discount)

Now i have to get the opening balance and closing balance for a particular day( for each item in the stock table).

i.e. If i require 8thJune details the the opening stock wwould be the prevoius day closing stock.

How should i do tht??? I'm not able to solve this..Somebody help me out please.......

nr
SQLTeam MVY

12543 Posts

Posted - 2006-06-08 : 05:30:08
Terrible naming standards and probably structure
what does stock hold? Is it the current stock level or a starting level.

this might help

select s.id, name = max(s.name), val = i.val - o.val
from stock s
join (select id = id_fk, val = sum(received) from StockIn where date <= @date group by id_fk) i
on s.id = i.id
join (select id = id_fk, val = sum(salesamount) from StockOut where date <= @date group by id_fk) o
on s.id = o.id


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2006-06-08 : 05:38:17
duplicate question
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=67394


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

bettyatolive
Starting Member

21 Posts

Posted - 2006-06-08 : 05:45:10
Stock table holds the current value of stock.
And what do you mean by "val"?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-06-08 : 06:11:10
val is the alias name given to sum(received)

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -