简体   繁体   中英

how to select columns with aggregate function sql server

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

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

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]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM