简体   繁体   English

在选择列表中,列“ tbl_Stock.Item_Name”无效,因为它既不包含在聚合函数中,也不包含在GROUP BY子句中

[英]Column 'tbl_Stock.Item_Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause

I have a database table named as tbl_stock. 我有一个名为tbl_stock的数据库表。

eg: 例如:

Order_ID  Item_Name  Item_Stock  Item_Dispatched  Item_Avaliable 
........  .......... ..........  ...............  ..............
1           abc           10            2               8
2           abc           13            7               6
3           abc           23            10              13
4           xyz           43            12              31
5           xyz            4            1               3

I want to get an out put in gridview as shown below: 我想在网格视图中放置一个输出,如下所示:

Item_Name  Item_Stock  Item_Dispatched  Item_Avaliable 
.........  ..........  ...............  ..............
abc           46            19              27
xyz           47            13              33

I want to calculate specific column values. 我想计算特定的列值。 While using the given below SQL query showing one error. 当使用下面给出的SQL查询时,显示一个错误。 The error is " Column 'tbl_Stock.Item_Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. " 错误是“ Column 'tbl_Stock.Item_Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

SQL : SQL:

SELECT DISTINCT Item_Name, 
COUNT(Item_Stock) AS Item_Stock, 
COUNT(Item_Dispatched) AS Item_Dispatched, 
COUNT(Item_Avaliable) AS Item_Avaliable 
FROM tbl_Stock

Add group by clause rather than distinct like: 添加group by子句,而不是像下面这样区分:

SELECT Item_Name, 
SUM(Item_Stock) AS Item_Stock, 
SUM(Item_Dispatched) AS Item_Dispatched, 
SUM(Item_Avaliable) AS Item_Avaliable 
FROM tbl_Stock
GROUP BY Item_Name

It will group all your records by Item_Name and will do SUM on each and every column. 它将按Item_Name将所有记录分组,并对每一列进行SUM运算。

as the error says, add GROUP BY to calculate totals for each item 如错误所示,添加GROUP BY以计算每个项目的总计

SELECT /*DISTINCT*/ Item_Name, 
SUM(Item_Stock) AS Item_Stock, 
SUM(Item_Dispatched) AS Item_Dispatched, 
SUM(Item_Avaliable) AS Item_Avaliable 
FROM tbl_Stock
GROUP BY Item_Name

also there will be no need in DISTINCT. 在DISTINCT中也将不需要。

COUNT() function will return number or rows (3 for abc item in Item_Stock, Item_Dispatched and Item_Avaliable columns and 2 for xyz), so to get desired result change it and use SUM() COUNT()函数将返回数字或行(对于Item_Stock,Item_Dispatched和Item_Avaliable列中的abc项目为3,对于xyz为2),因此要获得所需的结果,请对其进行更改并使用SUM()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL:“列&#39;tbl.column&#39;在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。” - SQL: “Column 'tbl.column' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.” 列“Users.Name”在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中 - Column 'Users.Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause 列“faculty.f_name”在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中 - Column 'faculty.f_name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause 选择列表中的“ Product.product_name”列无效,因为它既不包含在聚合函数中,也不包含在GROUP BY子句中 - Column 'Product.product_name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause 柱 <name> 在选择列表中无效,因为它未包含在聚合函数或GROUP BY子句中 - Column <name> is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause 选择列表中的“ Course.Course_Name”列无效,因为它既不包含在聚合函数中也不在GROUP BY子句中 - Column 'Course.Course_Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause 列 'City.Name' 在 select 列表中无效,因为它不包含在聚合 function 或 GROUP BY 子句中 - Column 'City.Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause 列 'seller.seller_Name' 在 select 列表中无效,因为它不包含在聚合 function 或 GROUP BY 子句中 - Column 'seller.seller_Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause 在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中 - invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause GROUP BY给出“列在选择列表中无效,因为该列未包含在聚合函数或GROUP BY子句中” - GROUP BY gives “Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM