我正在尝试学习如何在图表中显示数据,到目前为止我已设法显示数据,但我遇到了我的SQL查询问题,我需要它来向我展示数据以某种方式。
这是我的代码>我从我选择商店名称和2个日期时间选择器中获得了1个组合框。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Provider As String
Provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
Dim dataFile As String
dataFile = "Original.accdb"
Dim SqlQry1 As String = "SELECT [Delivered], [Datess] FROM [Table1] WHERE [Store] = @1 AND [Datess] BETWEEN @2 AND @3 ORDER BY [Datess] ASC"
Using myconnection As New OleDbConnection(Provider & dataFile)
myconnection.Open()
Dim cmd1 As New OleDbCommand(SqlQry1, myconnection)
cmd1.Parameters.Add(New OleDbParameter With {.ParameterName = "@1", .OleDbType = OleDbType.Char, .Value = combobox1.SelectedItem})
cmd1.Parameters.Add(New OleDbParameter With {.ParameterName = "@2", .OleDbType = OleDbType.Date, .Value = fromdatess.Value.Date})
cmd1.Parameters.Add(New OleDbParameter With {.ParameterName = "@3", .OleDbType = OleDbType.Date, .Value = todatess.Value.Date})
Dim dr As OleDbDataReader = cmd1.ExecuteReader
While dr.Read
Dim asd As String
asd = dr("Delivered")
Dim datess As Date
datess = dr("Datess")
Chart1.ChartAreas(0).AxisX.Interval = 1
Chart1.Series("Series1").Points.AddXY(datess.ToShortDateString, asd)
End While
End Using
End Sub
我希望能够总结" Delivered" 现在我的桌子就像这样>
ID | Store | Delivered | Datess
1 Ace1 200 3/1/2018
2 Ace1 150 3/1/2018
3 Bis1 50 3/2/2018
4 Ace1 50 3/2/2018
我想要做的就是像这样显示>
Ace1 350 3/1/2018
Bis1 50 3/2/2018
Ace1 50 3/2/2018
正如你所看到的,Ace1总和了。现在我已经尝试对它进行求和,但它会显示所有日子的总和结果,因为我有" BETWEEN Datess和Datess"所以这些日期之间的总结。我怎样才能只计算当天的数据,然后显示整个月的1/3/2018到1/30/2018的数据。任何帮助将受到高度赞赏。
感谢〜
答案 0 :(得分:1)
我认为您需要使用SUM
和GROUP BY
如果您想阅读SUM([Delivered])
,请为该字段提供别名。
SELECT [Store],SUM([Delivered]) AS Delivered, [Datess]
FROM [Table1]
WHERE [Store] = @1 AND [Datess] BETWEEN @2 AND @3
GROUP BY [Store],[Datess]
ORDER BY [Datess] ASC