2015-09-03 93 views
0

我有5個數據表具有每個3列(ID,品牌,Quanitiy)派生開放,購買,銷售和閉幕股票。前3個數據表應該合併並總結起來,以得出在特定時期的初始股票。第4個數據表用於在給定期間內完成的採購。最後一個Datatable用於在給定期間內完成銷售。相同的數據表2列合併和總結vb.net中的一列

這裏是我的問題:

  1. 如何合併3個數據表,並使用第一2列求和值是多少?

  2. 我如何在水晶報表中使用表格?

我的代碼是:

Dim con As New ClassConnection 
    If con.Conn.State = ConnectionState.Closed Then con.Conn.Open() 

    'To get Opening Stock in Full Quantity 
    Dim sql As String = "SELECT tblBrand.B_ID AS ID, tblBrand.B_Name AS Brand," & _ 
    " Sum (tblOp_Details.Net_Qty) AS Quantity FROM tblOp_Stock INNER JOIN (tblBrand INNER JOIN" & _ 
    " tblOp_Details ON tblBrand.B_ID = tblOp_Details.B_ID) ON tblOp_Stock.Stk_ID = tblOp_Details.Stk_ID" & _ 
    " WHERE tblOp_Stock.God_ID = @GID GROUP BY tblBrand.B_ID, tblBrand.B_Name" 

    'To get Purchases < start date and adding to the opening stock 
    Dim sql1 As String = "SELECT tblBrand.B_ID AS ID, tblBrand.B_Name AS Brand," & _ 
    " Sum (tblPur_Details.Net_Qty) AS Quantity FROM tblPurchase INNER JOIN (tblBrand INNER JOIN" & _ 
    " tblPur_Details ON tblBrand.B_ID = tblPur_Details.B_ID) ON tblPurchase.Pur_ID = tblPur_Details.Pur_ID" & _ 
    " WHERE tblPurchase.God_ID = @GID AND tblPurchase.Rec_Date < @SDate GROUP BY tblBrand.B_ID, tblBrand.B_Name" 

    'To get Sales < Start date and subtracting to the above 
    Dim sql2 As String = "SELECT tblBrand.B_ID AS ID, tblBrand.B_Name AS Brand," & _ 
    " Sum (tblSales_Details.Net_Qty) AS Quantity FROM tblSales INNER JOIN (tblBrand INNER JOIN" & _ 
    " tblSales_Details ON tblBrand.B_ID = tblSales_Details.B_ID) ON tblSales.Sale_ID = tblSales_Details.Sale_ID" & _ 
    " WHERE tblSales.God_ID = @GID AND tblSales.Sale_Date < @SDate GROUP BY tblBrand.B_ID, tblBrand.B_Name" 
    'The above 3 condition is for deriving opening stock as on given date 

    'To get Purchases >= Start Date and <= Start Date 
    Dim sql3 As String = "SELECT tblBrand.B_ID AS ID, tblBrand.B_Name AS Brand," & _ 
    " Sum(tblPur_Details.Net_Qty) AS Quantity FROM tblBrand INNER JOIN (tblPurchase INNER JOIN" & _ 
    " tblPur_Details ON tblPurchase.Pur_ID = tblPur_Details.Pur_ID) ON tblBrand.B_ID = tblPur_Details.B_ID" & _ 
    " WHERE tblPurchase.God_ID = @GID And tblPurchase.Rec_Date >= @SDate And tblPurchase.Rec_Date" & _ 
    " <= @EDate GROUP BY tblBrand.B_ID, tblBrand.B_Name" 
    'The above condition is for deriving Purchases as on given date 

    'To get Sales >= Start Date and <= Start Date 
    Dim sql4 As String = "SELECT tblBrand.B_ID AS ID, tblBrand.B_Name AS Brand," & _ 
    " Sum(tblSales_Details.Net_Qty) AS Quantity FROM tblBrand INNER JOIN (tblSales INNER JOIN" & _ 
    " tblSales_Details ON tblSales.Sale_ID = tblSales_Details.Sale_ID) ON tblBrand.B_ID = tblSales_Details.B_ID" & _ 
    " WHERE tblSales.God_ID = @GID And tblSales.Sale_Date >= @SDate And tblSales.Sale_Date <= @EDate" & _ 
    " And tblSales_Details.S_Active = @SAct GROUP BY tblBrand.B_ID, tblBrand.B_Name" 
    'The above condition is for deriving Sales as on given date 

    Dim da As New OleDb.OleDbDataAdapter(sql, con.Conn) 
    Dim da1 As New OleDb.OleDbDataAdapter(sql1, con.Conn) 
    Dim da2 As New OleDb.OleDbDataAdapter(sql2, con.Conn) 
    Dim da3 As New OleDb.OleDbDataAdapter(sql3, con.Conn) 
    Dim da4 As New OleDb.OleDbDataAdapter(sql4, con.Conn) 

    da.SelectCommand.Parameters.AddWithValue("@GID", Me.stBar_G_ID.Text) 
    da1.SelectCommand.Parameters.AddWithValue("@GID", Me.stBar_G_ID.Text) 
    da1.SelectCommand.Parameters.AddWithValue("@SDate", Me.dtpStart.ToString) 
    da2.SelectCommand.Parameters.AddWithValue("@GID", Me.stBar_G_ID.Text) 
    da2.SelectCommand.Parameters.AddWithValue("@SDate", Me.dtpStart.ToString) 
    da3.SelectCommand.Parameters.AddWithValue("@GID", Me.stBar_G_ID.Text) 
    da3.SelectCommand.Parameters.AddWithValue("@SDate", Me.dtpStart.ToString) 
    da3.SelectCommand.Parameters.AddWithValue("@EDate", Me.dtpEnd.ToString) 
    da4.SelectCommand.Parameters.AddWithValue("@GID", Me.stBar_G_ID.Text) 
    da4.SelectCommand.Parameters.AddWithValue("@SDate", Me.dtpStart.ToString) 
    da4.SelectCommand.Parameters.AddWithValue("@EDate", Me.dtpEnd.ToString) 
    da4.SelectCommand.Parameters.AddWithValue("@SAct", "Yes") 

    Dim dt As New DataTable 
    Dim dt1 As New DataTable 
    Dim dt2 As New DataTable 
    Dim dt3 As New DataTable 
    Dim dt4 As New DataTable 
+0

_「哪有我在水晶報表中使用表格「_不要在一個問題上詢問多個問題,而是單獨提問。 –

+0

是否要連接三個表格,然後按ID,Brand分組以獲得每個組的Quanitiy總和? –

+0

您是否嘗試過使用子報告 – Siva

回答

0

忽略了一個事實,這種行動應在數據庫而不是內存中完成,你可以使用Linq-To-DataSet這是Linq-To-Objects一個子集。

如果你想連接的三個表,按ID + Brand得到的Quantity總和:

Dim allRows = dt1.AsEnumerable().Concat(dt2.AsEnumerable()).Concat(dt3.AsEnumerable()) 
Dim query = From row In allRows 
      Let GroupColumns = New With { 
       .Id = row.Field(Of Int32)("Id"), 
       .Brand = row.Field(Of String)("Brand") 
      } 
      Group row By GroupColumns Into Group 
      Select New With { 
       .Id = GroupColumns.Id, 
       .Brand = GroupColumns.Brand, 
       .SumQuantity = Group.Sum(Function(row) row.Field(Of Int32)("Quantity")) 
      } 

從查詢填充合併表:

Dim dt As DataTable = dt1.Clone() ' empty, same columns 
For Each x In query 
    Dim row = dt.Rows.Add() 
    row.SetField("Id", x.Id) 
    row.SetField("Brand", x.Brand) 
    row.SetField("Quantity", x.SumQuantity) 
Next 
相關問題