Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Is there sort of SQL command that will make a sum

Author  Topic 

Xarzu
Starting Member

25 Posts

Posted - 2011-09-15 : 20:58:26
Is there sort of SQL command that will make a sum of some of the data in one column provided that the data in other columns are the same -- for example, all of the sales from one month added up?

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-09-15 : 22:16:44
Sure. You want to use the SUM() function and then GROUP BY.

Dates can get a little tricky but not too bad.

This would show total sales for August:
SELECT	Sum(sales)
FROM yourTable
WHERE datepart(year, saledate) = 2011
AND datepart(month, saledate) = 8

This will show all sales, broken down ny month and year
SELECT 	DatePart(month, saledate) as month, DatePart(year, SaleDate) as year, SUM(sales) as totalSales
FROM sales
GROUP BY
DatePart(Month, SaleDate), DatePart(year, SaleDate)
ORDER BY
year, month;
Go to Top of Page
   

- Advertisement -