Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.net中的聚合SQL查询_Sql_Vb.net_Ms Access - Fatal编程技术网

VB.net中的聚合SQL查询

VB.net中的聚合SQL查询,sql,vb.net,ms-access,Sql,Vb.net,Ms Access,我有以下疑问: 插入tbl\u集团发票 选择tbl_Invoices.CustomerName作为CustomerName, tbl_Invoices.CountryCode作为CountryCode, tbl_发票。[组]作为[组], 总计((tbl_发票.PageReferenceVolume/tbl_发票.DaysInMonth)*tbl_发票.ActiveDaysInMonth)作为PageReferenceVolume, 总计(tbl_Invoices.BlackPages+tbl_I

我有以下疑问:

插入tbl\u集团发票
选择tbl_Invoices.CustomerName作为CustomerName,
tbl_Invoices.CountryCode作为CountryCode,
tbl_发票。[组]作为[组],
总计((tbl_发票.PageReferenceVolume/tbl_发票.DaysInMonth)*tbl_发票.ActiveDaysInMonth)作为PageReferenceVolume,
总计(tbl_Invoices.BlackPages+tbl_Invoices.ColorPages)作为实际总体积,
实际页面音量/页面参考音量作为UsageRate
来自tbl_发票
按tbl_发票进行分组。客户名称,tbl_发票。国家代码,tbl_发票。[组]
当我在Ms Access 2013中直接执行它时,它工作得非常好。 但是,当嵌入我的VB.net代码时,我会收到一条错误消息,上面说:

您的查询不包括指定的表达式 “ActualPageVolume/PageReferenceVolume”作为聚合函数的一部分

这是我的vb代码

cmd.Connection=mdlLocalAccDB.accessConn
cmd.CommandType=CommandType.Text
尝试
squry=String.Empty
sQuery=sQuery&“插入tbl\u集团发票”
sQuery=sQuery&“选择tbl_Invoices.CustomerName作为CustomerName,”
squry=squry&“tbl_Invoices.CountryCode作为CountryCode,”
sQuery=sQuery&“待处理发票。[组]作为[组],”
squry=squry&“总计((tbl_Invoices.PageReferenceVolume/tbl_Invoices.DaysInMonth)*tbl_Invoices.ActiveDaysInMonth)作为PageReferenceVolume,”
squry=squry&“总计(tbl_Invoices.BlackPages+tbl_Invoices.ColorPages)作为实际页面容量,”
squry=squry&“ActualPageVolume/PageReferenceVolume作为UsageRate”
sQuery=sQuery&“来自待结算发票”
squry=squry&“按tbl_发票分组。客户名称,tbl_发票。国家代码,tbl_发票。[组]”
cmd.CommandText=sQuery
QueryReturn=cmd.ExecuteNonQuery()
特例
MsgBox(“准备发票:开票步骤6”&vbCrLf&ErrorToString())
出口接头
结束尝试

有什么想法吗?(除非最好使用参数而不是字符串连接。

通常,在SQL中,您不能在同一
select
子句中定义列别名的其他部分引用它们。我很惊讶Access在直接运行查询时支持这一点

这将是运行该查询的更常见的方式(我刚刚用相应的表达式替换了
ActualPageVolume/PageReferenceVolume
):

插入tbl\u集团发票
选择tbl_Invoices.CustomerName作为CustomerName,
tbl_Invoices.CountryCode作为CountryCode,
tbl_发票。[组]作为[组],
总计((tbl_发票.PageReferenceVolume/tbl_发票.DaysInMonth)*tbl_发票.ActiveDaysInMonth)作为PageReferenceVolume,
总计(tbl_Invoices.BlackPages+tbl_Invoices.ColorPages)作为实际总体积,
金额(tbl_Invoices.BlackPages+tbl_Invoices.ColorPages)/金额((tbl_Invoices.PageReferenceVolume/tbl_Invoices.DaysInMonth)*tbl_Invoices.ActiveDaysInMonth)作为使用日期
来自tbl_发票
按tbl_发票进行分组。客户名称,tbl_发票。国家代码,tbl_发票。[组]
希望效果更好

关于你最后的小纸条:

不过最好使用参数而不是字符串连接


您当前的查询没有任何需要参数绑定的内容。您现在使用的方式完全可以。

通常,在SQL中,您不能在定义列别名的同一
select
子句的其他部分中引用列别名。我很惊讶Access在直接运行查询时支持这一点

这将是运行该查询的更常见的方式(我刚刚用相应的表达式替换了
ActualPageVolume/PageReferenceVolume
):

插入tbl\u集团发票
选择tbl_Invoices.CustomerName作为CustomerName,
tbl_Invoices.CountryCode作为CountryCode,
tbl_发票。[组]作为[组],
总计((tbl_发票.PageReferenceVolume/tbl_发票.DaysInMonth)*tbl_发票.ActiveDaysInMonth)作为PageReferenceVolume,
总计(tbl_Invoices.BlackPages+tbl_Invoices.ColorPages)作为实际总体积,
金额(tbl_Invoices.BlackPages+tbl_Invoices.ColorPages)/金额((tbl_Invoices.PageReferenceVolume/tbl_Invoices.DaysInMonth)*tbl_Invoices.ActiveDaysInMonth)作为使用日期
来自tbl_发票
按tbl_发票进行分组。客户名称,tbl_发票。国家代码,tbl_发票。[组]
希望效果更好

关于你最后的小纸条:

不过最好使用参数而不是字符串连接


您当前的查询没有任何需要参数绑定的内容。您现在使用的方式完全可以。

由于这是一个插入,请尝试删除AS PAGEREFERENCEPOLVOLUME和AS ACTIVALPAGEVOLUME,因为它们本来就不是必需的。如果它不能解决问题,至少可以为您提供一个更好的错误线索。哦,j我们读了@sstan comment,我想他是对的。我第一次读的时候没有注意到。因为这是一个插入,所以请尝试删除AS PageReferenceVolume和AS ActualPageVolume,因为它们本来就不需要。如果它不能解决问题,至少可以给你一个更好的错误线索。哦,只要读一下@sstan comment,我就知道了hink he是对的。当我第一次阅读它时,我没有领会到这一点。MS Access Jet/ACE SQL方言确实可以在同一个查询中引用计算列。但是,我无法复制OP的问题。我尝试在.mdb/.accdb版本上使用ADO和DAO来处理
SELECT
INSERT
语句。我可以成功引用命名为计算列的co聚合查询中的列。@L.Moronvalle-您的驱动程序/提供者是什么?版本?提供者=Microsoft.ACE.OLEDB.12.0MS Access Jet/ACE SQL方言确实可以引用同一查询中的计算列。H