加入4个不同条件的表

时间:2017-04-16 13:26:50

标签: sql vb.net ms-access

我使用vb.net 2012和msaccess2016作为数据库。我有4张桌子。物料主,关闭,购买,发行。现在我想在下面格式化的datagridview中得到一段时间的结果。以下是专栏。

  • 项目名称(物品主数据)
  • 类别(物品主)
  • 单位(物品主)
  • 数量总和(结束)
  • 数量总和(购买)
  • 数量总和(问题)

我已经使用Item master和Closing创建了一个查询,并且它正在运行。但是当我尝试购买或发行时,没有数据。购买和发行表没有数据。

Dim sdate, edate As Date
    sdate = dtsdate.Value
    edate = dtedate.Value

    Dim sqlSelect As String = "SELECT itemmaster.[Item Name], itemmaster.Category, itemmaster.Unit," _
                              & " Sum(Closing.Qty) AS Opening FROM itemmaster INNER JOIN Closing ON " _
                              & "itemmaster.[Item Name] = Closing.[Item Name] WHERE(((Closing.closeDate) >= #" & sdate & " # And (Closing.closeDate) <=#" & edate & " #))" _
                              & " GROUP BY itemmaster.[Item Name], itemmaster.Category, itemmaster.Unit"


    Try
        Dim con As New Odbc.OdbcConnection
        con.ConnectionString = "Dsn=sdbinventory;"
        Dim dt As New DataTable("user2")
        Using cmd = New Odbc.OdbcCommand(sqlSelect, con)
            con.Open()
            Dim da As New System.Data.Odbc.OdbcDataAdapter(sqlSelect, con)
            da.Fill(dt)
            con.Close()
        End Using
        dgtvreport.DataSource = dt
    Catch ex As Exception

        Throw
    End Try

请你帮我添加两列。 感谢

os.path.join()

我的查询

    PARAMETERS sdate DateTime, edate DateTime;
SELECT itemmaster.[Item Name], itemmaster.Category, itemmaster.Unit, Sum(Closing.Qty) AS SumOfQty1, Sum(Purchase.Qty) AS SumOfQty, Sum(Issue.Quantity) AS SumOfQuantity
FROM ((itemmaster INNER JOIN Closing ON itemmaster.[Item Name] = Closing.[Item Name]) INNER JOIN Issue ON itemmaster.[Item Name] = Issue.[Item Name]) INNER JOIN Purchase ON itemmaster.[Item Name] = Purchase.[Item Name]
WHERE (((Closing.closeDate)>=[sdate] And (Closing.closeDate)<=[edate]) AND ((Purchase.PDate)>=[sdate] And (Purchase.PDate)<=[edate]) AND ((Issue.Idate)>=[sdate] And (Issue.Idate)<=[edate]))
GROUP BY itemmaster.[Item Name], itemmaster.Category, itemmaster.Unit;

我试图在访问中运行它。还添加了样本数据。我只想从itemmaster导入所有项目名称,类别和单位。现在我想在下一栏添加开盘股并购买和发行。我想创建和逐项报告。

Item Name      Category     Unit     Opening     Purchased     Issued   Balance

  item1         Grocery      Kg        2           3               4       1

  item2         beverages    PCS       0           1               1       0

  Item3         vegetables   kg        2           0               0       2

  item4         consumable   Pkt       10          2               1       11

1 个答案:

答案 0 :(得分:0)

@ june7是的我相信你的最佳答案也是我所知道的,但我已经搜索过是否有其他选择,在这个过程中你真的给了我关于不同类型查询的深入知识和差异。感谢。