如何用聚合函数sql server选择列

时间:2021-11-17 22:34:32

i need to select two columns.1. calculate sum of one column and display it 2.display column as it is. so i tried below code

我需要选择两列。计算一列的总和并显示它2.显示列。所以我尝试下面的代码

SELECT Sum(CONVERT(FLOAT, Replace(total, Char(0), ''))) AS Total, 
       [product name] 
FROM   tb_sales_entry_each_product 
GROUP  BY [sales date] 

error message

错误信息

Column 'tb_sales_entry_each_product.Product Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

where i made error.thanks

在哪里我犯了错误。谢谢

2 个解决方案

#1


1  

just need to group

只需要分组

select SUM(CONVERT(float, REPLACE(Total, CHAR(0), ''))) as Total, [Product Name]  
from tb_sales_entry_each_product group by [Sales Date], [product name]

When ever you do a numercial count sum etc, any other columns need to be grouped. thats all your missing

如果您进行了商业计数总和等,则需要对任何其他列进行分组。这就是你所有的遗失

#2


1  

Try this:

尝试这个:

select SUM(CONVERT(float, REPLACE(Total, CHAR(0), ''))) as Total, 
[Product Name]  ,[Sales Date]
from tb_sales_entry_each_product 
group by [Sales Date],[Product Name]

#1


1  

just need to group

只需要分组

select SUM(CONVERT(float, REPLACE(Total, CHAR(0), ''))) as Total, [Product Name]  
from tb_sales_entry_each_product group by [Sales Date], [product name]

When ever you do a numercial count sum etc, any other columns need to be grouped. thats all your missing

如果您进行了商业计数总和等,则需要对任何其他列进行分组。这就是你所有的遗失

#2


1  

Try this:

尝试这个:

select SUM(CONVERT(float, REPLACE(Total, CHAR(0), ''))) as Total, 
[Product Name]  ,[Sales Date]
from tb_sales_entry_each_product 
group by [Sales Date],[Product Name]