SQL按照日、周、月、年统计数据的方法分享

--按日
select sum(consume),day([date]) from consume_record where year([date]) = '2006' group by day([date])

--按周quarter
select sum(consume),datename(week,[date]) from consume_record where year([date]) = '2006' group by datename(week,[date])

--按月
select sum(consume),month([date]) from consume_record where year([date]) = '2006' group by month([date])

--按季
select sum(consume),datename(quarter,[date]) from consume_record where year([date]) = '2006' group by datename(quarter,[date])

--按年
select sum(consume),year([date]) from consume_record where  group by year([date])

DATE_FORMAT

select DATE_FORMAT(create_time,'%Y%u') weeks,count(caseid) count from tc_case group by weeks; select DATE_FORMAT(create_time,'%Y%m%d') days,count(caseid) count from tc_case group by days; select DATE_FORMAT(create_time,'%Y%m') months,count(caseid) count from tc_case group by months;

DATE_FORMAT(date,format)
根据format字符串格式化date值。下列修饰符可以被用在format字符串中:
%M 月名字(January……December)
%W 星期名字(Sunday……Saturday)
%D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)
%Y 年, 数字, 4 位
%y 年, 数字, 2 位
%a 缩写的星期名字(Sun……Sat)
%d 月份中的天数, 数字(00……31)
%e 月份中的天数, 数字(0……31)
%m 月, 数字(01……12)
%c 月, 数字(1……12)
%b 缩写的月份名字(Jan……Dec)
%j 一年中的天数(001……366)
%H 小时(00……23)
%k 小时(0……23)
%h 小时(01……12)
%I 小时(01……12)
%l 小时(1……12)
%i 分钟, 数字(00……59)
%r 时间,12 小时(hh:mm:ss [AP]M)
%T 时间,24 小时(hh:mm:ss)
%S 秒(00……59)
%s 秒(00……59)
%p AM或PM
%w 一个星期中的天数(0=Sunday ……6=Saturday )
%U 星期(0……52), 这里星期天是星期的第一天
%u 星期(0……52), 这里星期一是星期的第一天
%% 一个文字“%”。

本文只是记录在项目中用到的统计的SQL语句,记一笔以防忘了

/// <summary> /// 获取统计数据 /// </summary> /// <param name="CKEY">店面ckey</param> /// <param name="type">统计类型(日、周、月、年)</param> /// <returns></returns> [WebMethod(true)] public static string GetData3(string CKEY, string type) { StringBuilder strSql = new StringBuilder(); #region SQL语句 if (type == "0") { #region 日 strSql.AppendFormat(" WITH WeekDate "); strSql.AppendFormat(" AS ( SELECT DATEADD(d, -DAY(GETDATE()) + 1, GETDATE()) AS riqi "); strSql.AppendFormat(" UNION ALL "); strSql.AppendFormat(" SELECT riqi + 1 FROM WeekDate "); strSql.AppendFormat(" WHERE riqi + 1 <= ( SELECT DATEADD(d, -DAY(GETDATE()), DATEADD(m, 1, GETDATE())) ) "); strSql.AppendFormat(" ) "); strSql.AppendFormat(" SELECT CONVERT(CHAR(8), a.riqi, 112) AS 日 ,DAY (CONVERT(CHAR(8), a.riqi, 112)) AS DDay, "); strSql.AppendFormat(" ISNULL(tbB.日成交量, 0) AS 日成交量 , "); strSql.AppendFormat(" CASE WHEN CONVERT(CHAR(8), a.riqi, 112) > CONVERT(CHAR(8), GETDATE(), 112) "); strSql.AppendFormat(" THEN NULL "); strSql.AppendFormat(" WHEN CONVERT(CHAR(8), a.riqi, 112) <= CONVERT(CHAR(8), GETDATE(), 112) "); strSql.AppendFormat(" THEN ISNULL(tbB.日成交量, 0) "); strSql.AppendFormat(" END AS 日成交数量 , "); strSql.AppendFormat(" tbB.日实收金额 , "); strSql.AppendFormat(" CASE WHEN CONVERT(CHAR(8), a.riqi, 112) > CONVERT(CHAR(8), GETDATE(), 112) "); strSql.AppendFormat(" THEN NULL "); strSql.AppendFormat(" WHEN CONVERT(CHAR(8), a.riqi, 112) <= CONVERT(CHAR(8), GETDATE(), 112) "); strSql.AppendFormat(" THEN ISNULL(tbB.日实收金额, 0) "); strSql.AppendFormat(" END AS 日实收金额2 "); strSql.AppendFormat(" FROM WeekDate a "); strSql.AppendFormat(" LEFT JOIN ( SELECT ( SELECT COUNT(1) "); strSql.AppendFormat(" FROM dbo.CustomerBase base "); strSql.AppendFormat(" WHERE CKEY = '{0}' ", CKEY); strSql.AppendFormat(" AND " + impomo.TotalConsumptionMon + " > 0 "); strSql.AppendFormat(" AND TargetDate = cus.TargetDate "); strSql.AppendFormat(" ) 日成交量 , "); strSql.AppendFormat(" ISNULL(( SELECT SUM(Total) "); strSql.AppendFormat(" FROM ( SELECT SUM(CONVERT(FLOAT, ISNULL(( pc1 + pc2 + pc3 + pc4 + pc5 ), 0))) AS Total "); strSql.AppendFormat(" FROM PaymentContent AS pay "); strSql.AppendFormat(" WHERE PayDate = cus.TargetDate "); strSql.AppendFormat(" AND pay.CKEY = '{0}' ", CKEY); strSql.AppendFormat(" UNION ALL "); strSql.AppendFormat(" SELECT SUM(CONVERT(FLOAT, ISNULL(RecMoney, 0))) AS Total "); strSql.AppendFormat(" FROM dbo.CardRecharge8 AS recharge "); strSql.AppendFormat(" WHERE RechargDate = cus.TargetDate "); strSql.AppendFormat(" AND recharge.CKEY = '{0}' ", CKEY); strSql.AppendFormat(" UNION ALL "); strSql.AppendFormat(" SELECT SUM(CONVERT(FLOAT, ISNULL(( pc1 + pc2 + pc3 + pc4 + pc5 ), 0))) AS Total "); strSql.AppendFormat(" FROM dbo.PaymentSwimming AS payswim "); strSql.AppendFormat(" WHERE PayDate = cus.TargetDate "); strSql.AppendFormat(" AND payswim.CKEY = '{0}' ", CKEY); strSql.AppendFormat(" UNION ALL "); strSql.AppendFormat(" SELECT SUM(CONVERT(FLOAT, ISNULL(( wp1 + wp2 + wp3 + wp4 + wp5 ), 0))) AS Total "); strSql.AppendFormat(" FROM WarePaymentContent AS ware "); strSql.AppendFormat(" WHERE PayDate = cus.TargetDate "); strSql.AppendFormat(" AND ware.CKEY = '{0}' ", CKEY); strSql.AppendFormat(" ) B "); strSql.AppendFormat(" ), 0) AS 日实收金额 , "); strSql.AppendFormat(" TargetDate 日 "); strSql.AppendFormat(" FROM dbo.CustomerBase cus "); strSql.AppendFormat(" WHERE YEAR(TargetDate) = YEAR(GETDATE()) "); strSql.AppendFormat(" AND MONTH(TargetDate) = MONTH(GETDATE()) "); strSql.AppendFormat(" GROUP BY TargetDate "); strSql.AppendFormat(" ) AS tbB ON CONVERT(CHAR(8), a.riqi, 112) = tbB.日 "); #endregion } else if (type == "1") { #region 周 strSql.AppendFormat(" WITH WeekDate "); strSql.AppendFormat(" AS ( SELECT DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0) AS riqi "); strSql.AppendFormat(" UNION ALL "); strSql.AppendFormat(" SELECT riqi + 1 FROM WeekDate "); strSql.AppendFormat(" WHERE riqi + 1 <= ( SELECT DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 6) ) "); strSql.AppendFormat(" ) "); strSql.AppendFormat(" SELECT CONVERT(CHAR(8), a.riqi, 112) AS 日 , "); strSql.AppendFormat(" DATENAME(weekday,CONVERT(CHAR(8), a.riqi, 112)) DDay, "); strSql.AppendFormat(" ISNULL(tbB.日成交量, 0) AS 日成交量 , "); strSql.AppendFormat(" CASE WHEN CONVERT(CHAR(8), a.riqi, 112) > CONVERT(CHAR(8), GETDATE(), 112) "); strSql.AppendFormat(" THEN NULL "); strSql.AppendFormat(" WHEN CONVERT(CHAR(8), a.riqi, 112) <= CONVERT(CHAR(8), GETDATE(), 112) "); strSql.AppendFormat(" THEN ISNULL(tbB.日成交量, 0) "); strSql.AppendFormat(" END AS 日成交数量 , "); strSql.AppendFormat(" tbB.日实收金额 , "); strSql.AppendFormat(" CASE WHEN CONVERT(CHAR(8), a.riqi, 112) > CONVERT(CHAR(8), GETDATE(), 112) "); strSql.AppendFormat(" THEN NULL "); strSql.AppendFormat(" WHEN CONVERT(CHAR(8), a.riqi, 112) <= CONVERT(CHAR(8), GETDATE(), 112) "); strSql.AppendFormat(" THEN ISNULL(tbB.日实收金额, 0) "); strSql.AppendFormat(" END AS 日实收金额2 "); strSql.AppendFormat(" FROM WeekDate a "); strSql.AppendFormat(" LEFT JOIN ( SELECT ( SELECT COUNT(1) "); strSql.AppendFormat(" FROM dbo.CustomerBase base "); strSql.AppendFormat(" WHERE CKEY = '{0}'", CKEY); strSql.AppendFormat(" AND " + impomo.TotalConsumptionMon + " > 0 "); strSql.AppendFormat(" AND TargetDate = cus.TargetDate "); strSql.AppendFormat(" ) 日成交量 , "); strSql.AppendFormat(" ISNULL(( SELECT SUM(Total) "); strSql.AppendFormat(" FROM ( SELECT SUM(CONVERT(FLOAT, ISNULL(( pc1 + pc2 + pc3 + pc4 + pc5 ), 0))) AS Total "); strSql.AppendFormat(" FROM PaymentContent AS pay "); strSql.AppendFormat(" WHERE PayDate = cus.TargetDate "); strSql.AppendFormat(" AND pay.CKEY = '{0}'", CKEY); strSql.AppendFormat(" UNION ALL "); strSql.AppendFormat(" SELECT SUM(CONVERT(FLOAT, ISNULL(RecMoney, 0))) AS Total "); strSql.AppendFormat(" FROM dbo.CardRecharge8 AS recharge "); strSql.AppendFormat(" WHERE RechargDate = cus.TargetDate "); strSql.AppendFormat(" AND recharge.CKEY = '{0}'", CKEY); strSql.AppendFormat(" UNION ALL "); strSql.AppendFormat(" SELECT SUM(CONVERT(FLOAT, ISNULL(( pc1 + pc2 + pc3 + pc4 + pc5 ), 0))) AS Total "); strSql.AppendFormat(" FROM dbo.PaymentSwimming AS payswim "); strSql.AppendFormat(" WHERE PayDate = cus.TargetDate "); strSql.AppendFormat(" AND payswim.CKEY = '{0}'", CKEY); strSql.AppendFormat(" UNION ALL "); strSql.AppendFormat(" SELECT SUM(CONVERT(FLOAT, ISNULL(( wp1 + wp2 + wp3 + wp4 + wp5 ), 0))) AS Total "); strSql.AppendFormat(" FROM WarePaymentContent AS ware "); strSql.AppendFormat(" WHERE PayDate = cus.TargetDate "); strSql.AppendFormat(" AND ware.CKEY = '{0}'", CKEY); strSql.AppendFormat(" ) B "); strSql.AppendFormat(" ), 0) AS 日实收金额 , "); strSql.AppendFormat(" TargetDate 日 "); strSql.AppendFormat(" FROM dbo.CustomerBase cus "); strSql.AppendFormat(" WHERE DATEPART(wk, TargetDate) = DATEPART(wk, GETDATE()) "); strSql.AppendFormat(" AND DATEPART(yy, TargetDate) = DATEPART(yy, GETDATE()) "); strSql.AppendFormat(" GROUP BY TargetDate "); strSql.AppendFormat(" ) AS tbB ON CONVERT(CHAR(8), a.riqi, 112) = tbB.日 "); #endregion } else if (type == "2") { #region 月 strSql.AppendFormat("SELECT YearMonth.月 , "); strSql.AppendFormat(" tb.月成交量 , "); strSql.AppendFormat(" CASE WHEN YearMonth.月 > MONTH(GETDATE()) THEN NULL "); strSql.AppendFormat(" WHEN YearMonth.月 <= MONTH(GETDATE()) THEN ISNULL(tb.月成交量, 0) "); strSql.AppendFormat(" END AS 月成交数量 , "); strSql.AppendFormat(" tb.月实收总金额 , "); strSql.AppendFormat(" CASE WHEN YearMonth.月 > MONTH(GETDATE()) THEN NULL "); strSql.AppendFormat(" WHEN YearMonth.月 <= MONTH(GETDATE()) THEN ISNULL(tb.月实收总金额, 0) "); strSql.AppendFormat(" END AS 月实收总金额2 "); strSql.AppendFormat(" FROM ( SELECT 1 AS 月 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 "); strSql.AppendFormat(" UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12 "); strSql.AppendFormat(" ) AS YearMonth "); strSql.AppendFormat(" LEFT JOIN ( SELECT ( SELECT COUNT(1) "); strSql.AppendFormat(" FROM dbo.CustomerBase base "); strSql.AppendFormat(" WHERE CKEY = '{0}' ", CKEY); strSql.AppendFormat(" AND " + impomo.TotalConsumptionMon + " > 0 "); strSql.AppendFormat(" AND MONTH(TargetDate) = MONTH(cus.TargetDate) "); strSql.AppendFormat(" ) 月成交量 , "); strSql.AppendFormat(" ISNULL(( SELECT SUM(Total) "); strSql.AppendFormat(" FROM ( SELECT SUM(CONVERT(FLOAT, ISNULL(( pc1 + pc2 + pc3 + pc4 + pc5 ), 0))) AS Total "); strSql.AppendFormat(" FROM PaymentContent AS pay "); strSql.AppendFormat(" WHERE MONTH(PayDate) = MONTH(cus.TargetDate) "); strSql.AppendFormat(" AND pay.CKEY = '{0}' ", CKEY); strSql.AppendFormat(" UNION ALL "); strSql.AppendFormat(" SELECT SUM(CONVERT(FLOAT, ISNULL(RecMoney, 0))) AS Total "); strSql.AppendFormat(" FROM dbo.CardRecharge8 AS recharge "); strSql.AppendFormat(" WHERE MONTH(RechargDate) = MONTH(cus.TargetDate) "); strSql.AppendFormat(" AND recharge.CKEY = '{0}' ", CKEY); strSql.AppendFormat(" UNION ALL "); strSql.AppendFormat(" SELECT SUM(CONVERT(FLOAT, ISNULL(( pc1 + pc2 + pc3 + pc4 + pc5 ), 0))) AS Total "); strSql.AppendFormat(" FROM dbo.PaymentSwimming AS payswim "); strSql.AppendFormat(" WHERE MONTH(PayDate) = MONTH(cus.TargetDate) "); strSql.AppendFormat(" AND payswim.CKEY = '{0}' ", CKEY); strSql.AppendFormat(" UNION ALL "); strSql.AppendFormat(" SELECT SUM(CONVERT(FLOAT, ISNULL(( wp1 + wp2 + wp3 + wp4 + wp5 ), 0))) AS Total "); strSql.AppendFormat(" FROM WarePaymentContent AS ware "); strSql.AppendFormat(" WHERE MONTH(PayDate) = MONTH(cus.TargetDate) "); strSql.AppendFormat(" AND ware.CKEY = '{0}' ", CKEY); strSql.AppendFormat(" ) B "); strSql.AppendFormat(" ), 0) AS 月实收总金额 , "); strSql.AppendFormat(" MONTH(TargetDate) 月 "); strSql.AppendFormat(" FROM dbo.CustomerBase cus "); strSql.AppendFormat(" WHERE YEAR(TargetDate) = YEAR(GETDATE()) "); strSql.AppendFormat(" GROUP BY MONTH(cus.TargetDate) "); strSql.AppendFormat(" ) AS tb ON YearMonth.月 = tb.月 "); #endregion } else if (type == "3") { #region 年 strSql.AppendFormat("SELECT ( SELECT COUNT(1) "); strSql.AppendFormat(" FROM dbo.CustomerBase base "); strSql.AppendFormat(" WHERE CKEY = '{0}' ", CKEY); strSql.AppendFormat(" AND " + impomo.TotalConsumptionMon + " > 0 "); strSql.AppendFormat(" AND YEAR(TargetDate) = YEAR(cus.TargetDate) "); strSql.AppendFormat(" ) 年成交量 , "); strSql.AppendFormat(" CONVERT(NVARCHAR(20),CONVERT(DECIMAL(18,2),ISNULL(( SELECT SUM(Total) "); strSql.AppendFormat(" FROM ( SELECT SUM(CONVERT(FLOAT, ISNULL(( pc1 + pc2 + pc3 + pc4 + pc5 ), 0))) AS Total "); strSql.AppendFormat(" FROM PaymentContent AS pay "); strSql.AppendFormat(" WHERE YEAR(PayDate) = YEAR(cus.TargetDate) "); strSql.AppendFormat(" AND pay.CKEY = '{0}' ", CKEY); strSql.AppendFormat(" UNION ALL "); strSql.AppendFormat(" SELECT SUM(CONVERT(FLOAT, ISNULL(RecMoney, 0))) AS Total "); strSql.AppendFormat(" FROM dbo.CardRecharge8 AS recharge "); strSql.AppendFormat(" WHERE YEAR(RechargDate) = YEAR(cus.TargetDate) "); strSql.AppendFormat(" AND recharge.CKEY = '{0}' ", CKEY); strSql.AppendFormat(" UNION ALL "); strSql.AppendFormat(" SELECT SUM(CONVERT(FLOAT, ISNULL(( pc1 + pc2 + pc3 + pc4 + pc5 ), 0))) AS Total "); strSql.AppendFormat(" FROM dbo.PaymentSwimming AS payswim "); strSql.AppendFormat(" WHERE YEAR(PayDate) = YEAR(cus.TargetDate) "); strSql.AppendFormat(" AND payswim.CKEY = '{0}' ", CKEY); strSql.AppendFormat(" UNION ALL "); strSql.AppendFormat(" SELECT SUM(CONVERT(FLOAT, ISNULL(( wp1 + wp2 + wp3 + wp4 + wp5 ), 0))) AS Total "); strSql.AppendFormat(" FROM WarePaymentContent AS ware "); strSql.AppendFormat(" WHERE YEAR(PayDate) = YEAR(cus.TargetDate) "); strSql.AppendFormat(" AND ware.CKEY = '{0}' ", CKEY); strSql.AppendFormat(" ) B "); strSql.AppendFormat(" ), 0))) AS 年实收总金额 , "); strSql.AppendFormat(" YEAR(TargetDate) 年 "); strSql.AppendFormat(" FROM dbo.CustomerBase cus "); strSql.AppendFormat(" GROUP BY YEAR(TargetDate) "); #endregion } #endregion DataTable table = DBHelper.GetDateTable(strSql.ToString()); string rs = Newtonsoft.Json.JsonConvert.SerializeObject(table); return rs; }

时间: 2024-10-26 17:18:30

SQL按照日、周、月、年统计数据的方法分享的相关文章

SQL按照日、周、月、年统计数据的方法分享_MsSql

--按日 select sum(consume),day([date]) from consume_record where year([date]) = '2006' group by day([date]) --按周quarter select sum(consume),datename(week,[date]) from consume_record where year([date]) = '2006' group by datename(week,[date]) --按月 select

Flex自定义组件开发之日周月日期选择日历控件

原文:Flex自定义组件开发之日周月日期选择日历控件           使用过DateField的我们都知道,DateField 控件是用于显示日期的文本字段,字段右侧带有日历图标.当用户在控件边框内的任一位置单击时,将弹出一个 DateChooser 控件,显示当月的所有日期.如果未选择日期,则该文本字段为空白,并且 DateChooser 控件中将显示当前日期的月份.当 DateChooser 控件处于打开状态时,用户可以在各个月份和年份之间滚动,并选择某个日期.选择日期后,DateCho

SQL Server 7.0数据库的六种数据移动方法

server|数据|数据库 SQL Server 7.0数据库的六种数据移动方法转自:世纪易网 2003年3月3日20:6本人从事的工作是数据库管理员,要维护多台服务器中的数据库,经常把某台服务器中的某个数据库移动到另外一台服务器,对数据的移动有些心得体会,希望和大家共同交流. 1. 通过工具DTS的设计器进行导入或导出 DTS的设计器功能强大,支持多任务,也是可视化界面,容易操作,但知道的人一般不多,如果只是进行SQL Server数据库中部分表的移动,用这种方法最好,当然,也可以进行全部表的

微信公众号统计数据中文章分享途径浅析

中介交易 http://www.aliyun.com/zixun/aggregation/6858.html">SEO诊断 淘宝客 云主机 技术大厅 微信公众号的统计功能让很多运营者有了衡量微信公众号运营好坏的依据,不过大部分微信运营者只关心用户增长数,图文页阅读数等数据,因为这些数据是老板关心的.对于更深入的数据则没有挖掘出来,而通过这些数据的分析,才能让我们更好的知道用户的喜好,从而也才能做出让用户喜欢的东西. 在微信图文分析中,有一个分享转发数据,里面包含了"朋友圈&quo

PHP实现的统计数据功能详解_php技巧

本文实例讲述了PHP实现的统计数据功能.分享给大家供大家参考,具体如下: 统计,就是把基本的数据,整合起来. 用到sql的,有group by 功能,count功能,order by功能等等. sql将收集的数据,进行统计分析. 一般情况下,sql处理后得到的数据,还要通过php的逻辑来进行整理. 以一定的格式,展示到前台. 一般都是以数组的方式展示,这也是数据结构的概念. 看这张图片,基本想想结构大概为 {上线数,出单总数,核过总数,总人均,总核率,{(坐席人1,工号1,出单数1,发货数1,核

delphi sql 语句-delphi-sql-语句 实现月统计数据

问题描述 delphi-sql-语句 实现月统计数据 有两个表, 表A: 日期 数量 2014-1-10 4 2014-1-23 5 2014-2-5 7 2014-3-10 4 2014-3-23 4 表B: 日期 数量 2014-1-10 30 2014-1-4 7 2014-1-31 35 2014-2-1 70 2014-2-30 60 2014-3-1 55 2014-3-23 45 2014-3-31 50 想要的结果: 统计时间段 2014-1-10~2014-3-23 月份 A.

MySQL中如何按月统计数据

表finance有俩个字段如下 date date money double(15,2) 下面需要对表 finance的2010年财务数据,按月进行统计 Sql代码 select DATE_FORMAT(date,'%Y-%m') as month,sum(money) as money from finance where DATE_FORMAT(date,'%Y')=2010 group by month order by month 下面是按周统计 查看MySQL的manual %X Ye

2014年12月桌面操作系统份额统计数据

每个月Net Applications都会发布上个月桌面操作系统份额统计数据. 这个数据可以帮助我们发现整体市场的趋势.根据这份统计数据显示,Windows 7今年1月份市占率微幅下降,从去年12月的56.26%下降至55.92%,Windows XP的市占率微幅上升,从去年12月的18.26%上升到18.93%,时,Windows8/8.1从13.52%上升至13.83%,而 Windows Vista下跌,从2.9%跌至2.44%. 另外值得注意的是,Windows10市占率现在已经出现在统

2011年9月Net Application关于全球浏览器市场排名统计数据

在2011年10月1日,网络监测机构Net Application发布的最新统计数据中表明,在9月份,微软IE浏览器的整体市场份额依旧是在继续下滑.而且IE市场的具体份额已经从8月份的55.31%下降到54.39%,而在其中,IE8已经从30.07%下降到29.91%;而IE7也由6.29%下降到了6.05%. 但是,备受瞩目的IE9仍旧保持增长,已经有越来越多的人都升级到了最新版本的IE浏览器.截至到九月末为止,在Windows 7的操作系统中,IE9在美国的市场份额已经上涨到了31%,在全球