如何分组并计算月份数据 [英] How to group by and count for month wise data

查看:62
本文介绍了如何分组并计算月份数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,其中包含一个日期时间列和许多其他列我希望计算每月列值的总和。请帮助

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

推荐答案

试试这个< br $>


Try this

select  DATENAME(mm,GETDATE()) as month,Sum(columnValue)
from tablename
Group by  DATENAME(mm,GETDATE())




如果你想包含其他具有month_name和值的列来总结,那么试试这个:



Hi,
If you want to include other columns with month_name and value to be sum up, then try this:

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


感谢您的解决方案我终于如下编写了我的查询并得到了想要的答案



Thanks for your solutions I have finally made up my query as below and got the desired answer

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])


这篇关于如何分组并计算月份数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆