[英]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.