我正在尝试使用组合框中的项目显示(日,周,月,年)的总销售额图表,作为从vb.net中的sql查询的过滤器。请帮忙。我的代码在下面,它会抛出错误,上面写着'找不到表0'。
cboxGroupBy.Items.Insert(0, "Day")
cboxGroupBy.Items.Insert(1, "Week")
cboxGroupBy.Items.Insert(2, "Month")
cboxGroupBy.Items.Insert(3, "Year")
SQL.ExecQuery("SELECT Date = DATEPART('" + Me.cboxGroupBy.Text + "', Date),
SUM (Amount) AS Amount
FROM Sales
GROUP BY DATEPART('" + Me.cboxGroupBy.Text + "', Date)")
Chart2.DataSource = SQL.SQLDS.Tables(0)
Chart2.Series.Clear()
Chart2.Series.Add("Total Revenue")
Chart2.Series("Total Revenue").XValueMember = "Date"
Chart2.Series("Total Revenue").YValueMembers = "Amount"
答案 0 :(得分:0)
看起来您正在尝试获取当前日期,因此在初始查询中使用GETDATE()(如果您的数据库是Sql Server)。如果还有别的,请查看如何获取当前日期。
所以:
SQL.ExecQuery("SELECT Date = DATEPART('" + Me.cboxGroupBy.Text + "', GETDATE()),
SUM (Amount) AS Amount
FROM Sales
GROUP BY DATEPART('" + Me.cboxGroupBy.Text + "', GETDATE())")
答案 1 :(得分:0)
从DatePart()
函数中删除单引号。无需在单引号中通过日或周等。这是您修改后的查询。
SQL.ExecQuery("SELECT Date = DATEPART(" + Me.cboxGroupBy.Text + ", Date),
SUM (Amount) AS Amount
FROM Sales
GROUP BY DATEPART(" + Me.cboxGroupBy.Text + ", Date)")