Click here to Skip to main content
15,921,577 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a table having a column of datetime and many others column I want to count sum of a column value month wise .Please help
Posted
Comments
Thanks7872 11-Dec-14 4:42am    
What have you tried so far?

Try this

SQL
select  DATENAME(mm,GETDATE()) as month,Sum(columnValue)
from tablename
Group by  DATENAME(mm,GETDATE())
 
Share this answer
 
Hi,
If you want to include other columns with month_name and value to be sum up, then try this:

SQL
select orderid, custid, orderdate, val, DateName(month, orderdate) as [Month],
        sum(val) OVER(partition by datename(month, orderdate) order by month(orderdate)) as [MonthTotal]
from Sales.OrderValues--source table
order by month(orderdate), orderid;--ordering by month so january, february, march...december will return in order
 
Share this answer
 
v2
Thanks for your solutions I have finally made up my query as below and got the desired answer

SQL
SELECT
      COUNT(*) as TotalCompleted,
      Year([SMSHistory_SendDateTime]) as InYear,
      DateName( month , DateAdd( month , Month([SMSHistory_SendDateTime]) , -1 ) ) as inMonth

  FROM [bzs_db].[dbo].[Qry_SMSHistory]
  WHERE [SMSHistory_IsComplete]=1
  group by Year([SMSHistory_SendDateTime]),Month([SMSHistory_SendDateTime])
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900