Connecting Tech Pros Worldwide Help | Site Map

Help for SQL query for calculation of Stock

Newbie
 
Join Date: Sep 2009
Posts: 17
#1: Sep 3 '09
I am using vb express and MsAccess as database.
I am trying to write a query for the calculation of Stock.
My tables are as under:
PId PDate ItemId Description Price Quantity Amount
1 28/8/2009 1 Coca Cola Normal 1,00 10 10,00
2 28/8/2009 2 Coca Cola Zero 1,00 5 5,00
3 29/8/2009 1 Coca Cola Normal 1,00 5 5,00
4 29/8/2009 2 Coca Cola Zero 1,00 10 10,00

Data in Sales Table is as under:

SId SDate ItemId Description Price Quantity Amount
1 30/8/2009 1 Coca Cola Normal 2,70 2 5,40
2 30/8/2009 2 Coca Cola Zero 2,70 3 7,10
3 31/8/2009 1 Coca Cola Normal 2,70 1 2,70
4 31/8/2009 2 Coca Cola Zero 2,70 2 2,70

I tried something like this to have balance of stock at any date or interval between two dates

Expand|Select|Wrap|Line Numbers
  1. Dim cmdText As String = "SELECT pt.ItemId, pt.Description, SUM(pt.Quantity)AS QuantityPurchased, SUM(st.Quantity) AS QuantitySold, (SUM(pt.Quantity) - SUM(st.Quantity)) AS Balance FROM PurchaseTable pt INNER JOIN SalesTable st ON pt.ItemId=st.ItemId WHERE pt.PDate Between @START and @END GROUP BY pt.ItemId, pt.Description" 
  2.  
  3.         If con.State = ConnectionState.Closed Then con.Open() 
  4.         Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(cmdText, con) 
  5.         cmd.CommandType = CommandType.Text 
  6.  
  7.         cmd.Parameters.AddWithValue("@START", OleDb.OleDbType.Date).Value = TextBox1.Text 
  8.         cmd.Parameters.AddWithValue("@END", OleDb.OleDbType.Date).Value = TextBox2.Text 
  9.  
  10.         Dim dr As OleDb.OleDbDataReader 
  11.  
  12.         If con.State = ConnectionState.Closed Then con.Open() 
  13.         dr = cmd.ExecuteReader 
  14.         If Not dr.HasRows Then 
  15.             MessageBox.Show("No Records Found for Date: " & TextBox1.Text) 
  16.         Else 
  17.             MessageBox.Show("Record found for Date: " & TextBox1.Text) 
  18.             ListView1.Items.Clear() 
  19.             ListView1.ForeColor = Color.DarkRed 
  20.             ListView1.GridLines = True 
  21.  
  22.             While dr.Read 
  23.                 Dim ls As New ListViewItem(dr.Item("ItemId").ToString()) 
  24.                 ls.SubItems.Add(dr.Item("Description").ToString()) 
  25.                 ls.SubItems.Add(dr.Item("QuantityPurchased").ToString()) 
  26.                 ls.SubItems.Add(dr.Item("QuantitySold").ToString()) 
  27.                 ls.SubItems.Add(dr.Item("Balance").ToString()) 
  28.                 ListView1.Items.Add(ls) 
  29.             End While 
  30.         End If 
But i could not get the desired results. Please advise what i am doing wrong with this.
Thanks
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#2: Sep 3 '09

re: Help for SQL query for calculation of Stock


Quote:
But i could not get the desired results.
This is a bit vague. The volunteers here don't know what you desire the results to be or look like. Do you get exception errors? Does the entire program crash? Or are the results just not formatted neatly? Does the query return nothing... return wrong results...

The better and more precisely you can define the actual problem the better someone can help.
Newbie
 
Join Date: Sep 2009
Posts: 17
#3: Sep 4 '09

re: Help for SQL query for calculation of Stock


Hi thanks for your reply. Sorry that i could not elaborate my question.

The problem i am having is as under:
Data in PurchaseTable

PId PDate ItemId Description Price Quantity Amount
1 28/8/2009 1 Coca Cola Normal 1,00 10 10,00
2 28/8/2009 2 Coca Cola Zero 1,00 5 5,00
3 29/8/2009 1 Coca Cola Normal 1,00 5 5,00
4 29/8/2009 2 Coca Cola Zero 1,00 10 10,00

Data in Sales Table is as under:

SId SDate ItemId Description Price Quantity Amount
1 30/8/2009 1 Coca Cola Normal 2,70 2 5,40
2 30/8/2009 2 Coca Cola Zero 2,70 3 7,10
3 31/8/2009 1 Coca Cola Normal 2,70 1 2,70
4 31/8/2009 2 Coca Cola Zero 2,70 2 2,70

The result of query with date range of 28-08-2009 to 29-08-2009 and also with the date range of 28-08-2009 to 31-08-2009 is displayed as under:

Description QuantityPurchased QuantitySold Balance
Coca Cola Normal 30 6 24
Coca Cola Zero 30 10 20

whereas actually for date range of 28-08-2009 to 29-08-2009 the result should be as under:

Coca Cola Normal 15 0 15
Coca Cola Zero 15 0 15

And with the date range of 28-08-2009 to 31-08-2009 the result should be as under:

Coca Cola Normal 15 3 12
Coca Cola Zero 15 5 10

Pleae advise what i am doing wrong with the query.
Reply

Tags
vb express


Similar Visual Basic .NET bytes