SQL周、日、月、年数据统计

本文只是记录在项目中用到的统计的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-11-08 23:15:40

SQL周、日、月、年数据统计的相关文章

C# 根据天、周、月汇总统计生成统计报表

先看核心代码: public List<DataEntity> SearchShopSalesReport(DateTimeOffset? dateFrom, DateTimeOffset? dateTo,string groupBy) { var query = data.DataEntity //这里data为DbContext .AsEnumerable(); if (dateFrom.HasValue) query = query.Where(x => x.Date >=

sql语句分别按日,按周,按月,按季统计金额

如: 表:consume_record 字段:consume (money类型) date (datetime类型) 请问怎么写四条sql语句分别按日,按周,按月,按季统计消费总量. 如:1月 1200元 2月 3400元 3月 2800元 --按日 select sum(consume),day([date]) from consume_record where year([date]) = '2006' group by day([date]) --按周quarter select sum(

Mysql按日、周、月进行分组统计

我们在用Mysql抽取数据时候,经常需要按照天.周.月等不同的粒度对数据进行分组统计.而我们的时间可能是“2017/12/5 0:0:0”这种准确的时间.所以在进行分组之前我们需要对时间进行一下处理. DATE_FORMAT是MySQL内置的一个函数,作用是以不同的格式显示日期/时间数据.具体的语法如下: DATE_FORMAT(date,format),其中 date:合法的日期.format:规定日期/时间的输出格式,其中format可使用的格式见文末链接. 下面我们通过具体例子来看如何通过

My97DatePicker日期控件实现按日、周、月、季、年选择时间段

 http://www.cnblogs.com/kevin-zlg/p/4631413.html 前台页面需要使用日期控件时,同事选择了My97DatePicker,功能还是挺强大的,在使用过程中需要实现按日.按周.按月.按季度.按年选择时间的功能. 控件自身提供了按日.按月和按年选择的功能,但是选择周和选择季度没有,鉴于方便,在选择周时就使用了选择日的状态,选择了日后再计算这一天所属的周的起始和结束日期. 在选择季度时,使用了选择月份的状态,选择月份后计算所属的季度的开始和结束日期. 控件本身

DB查询分析器7.01新增的周、月SQL执行计划功能

马根峰              (广东联合电子服务股份有限公司, 广州 510300) 1      引言   中国本土程序员马根峰(CSDN专访马根峰:海量数据处理与分析大师的中国本土程序员)推出的个人作品----万能数据库查询分析器,中文版本DB 查询分析器.英文版本<DB Query Analyzer>.它具有强大的功能.友好的操作界面.良好的操作性.跨越各种数据库平台乃至于EXCEL和文本文件. 你可以通过它 ① 查询ODBC数据源(包括世面上所有的数据库.TXT/CSV文件.EXC

sql按月统计数量和按月累加统计数量

1.简单的,按月统计数量 SELECT CREATE_DATE, DATE_FORMAT(CREATE_DATE, '%Y-%m') AS month , COUNT(*) AS sum FROM pt_user GROUP BY month; 2.按月累加统计数据 SELECT a.month, SUM(b.total) AS total FROM ( SELECT DATE_FORMAT(CREATE_DATE, '%Y-%m') AS month, SUM(sum) AS total FR

周平均月平均设计

http://blog.csdn.net/lili72 背景: 有些报表除了需要每天的数据走势图,有时还需要每周的平均和每月的平均,再和上周或月作比较,计算差值,然后分别形成周平均报表和月平均报表. 日报表:每天计算报表数据 周报表:每周某个时间只计算一次 月报表:每月每个时间只计算一次 以下设计可以作为比较通用的周月差值计算方式. 考虑到查询条件的跨年,所以设计表的时候有一个日期是标志某个周期内的数据,这样查询就能通过这个日期连续的查询.etldate 结果展示: 以下是详细代码,包括设计的表

ORACLE取周、月、季、年的开始时间和结束时间

 1           取周的开始时间和结束时间 取周的开始时间,以星期一为开始. SQL>SELECT TRUNC(TO_DATE('2013-11-25 10:31:11','YYYY-MM-DD HH24:MI:SS'),'D') + 1 ASA FROM DUAL; A ----------- 2013/11/25 取周的结束时间,以星期日为结束 SQL>SELECT TRUNC(TO_DATE('2013-11-25 10:31:11','YYYY-MM-DD HH24:MI:

Oracle日期转换为周,月,季度,半年,年

实现当前时间,周,月,季度,半年,年的转换,其中特别注意的是周的处理,IW方式处理逻辑,如果本周有大于等于4天,本周属于几年,如果小于4天,本周属于下一年 以1990/12/31为例,这天属于199101周 SELECT TO_NUMBER(TO_CHAR(SYSDATE, 'YYYYMMDD')) AS PERIOD,       SYSDATE AS P_DATE,       TO_NUMBER(TO_CHAR(SYSDATE, 'IYYYIW')) AS P_WEEK,       TO