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

前端之家收集整理的这篇文章主要介绍了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

sql;"> 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语句,记一笔以防忘了

sql;"> /// /// 获取统计数据 /// /// /// 统计类型(日、周、月、年) /// [WebMethod(true)] public static string GetData3(string CKEY,string type) { StringBuilder strsql = new StringBuilder();
  #region <a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>语句

  if (type == "0")
  {
    #region 日
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat(" WITH  WeekDate ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("     AS ( SELECT  DATEADD(d,-DAY(GETDATE()) + 1,GETDATE()) AS riqi ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("       UNION ALL ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("       SELECT  riqi + 1 FROM   WeekDate ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.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),112)) AS DDay,");
    strSql.AppendFormat("      ISNULL(tbB.日成交量,0) AS 日成交量,");
    strSql.AppendFormat("      CASE WHEN CONVERT(CHAR(8),112) > CONVERT(CHAR(8),GETDATE(),112) ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("        THEN NULL ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("        WHEN CONVERT(CHAR(8),112) <= CONVERT(CHAR(8),112) ");
    strSql.AppendFormat("        THEN ISNULL(tbB.日成交量,0) ");
    strSql.AppendFormat("      END AS 日成交数量,");
    strSql.AppendFormat("      tbB.日实收金额,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 ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                      AND TargetDate = cus.TargetDate ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                ) 日成交量,");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                ISNULL(( SELECT SUM(Total) ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                    FROM  ( SELECT  SUM(CONVERT(FLOAT,ISNULL(( pc1 + pc2 + pc3 + pc4 + pc5 ),0))) AS Total ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                         FROM   PaymentContent AS pay ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                         WHERE   PayDate = cus.TargetDate ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                              AND pay.CKEY = '{0}' ",CKEY);
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                         UNION ALL ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                         SELECT  SUM(CONVERT(FLOAT,ISNULL(RecMoney,0))) AS Total ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                         FROM   dbo.CardRecharge8 AS recharge ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                         WHERE   RechargDate = cus.TargetDate ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                              AND recharge.CKEY = '{0}' ",0))) AS Total ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                         FROM   dbo.PaymentSwimming AS payswim ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                         WHERE   PayDate = cus.TargetDate ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                              AND payswim.CKEY = '{0}' ",ISNULL(( wp1 + wp2 + wp3 + wp4 + wp5 ),0))) AS Total ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                         FROM   WarePaymentContent AS ware ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                         WHERE   PayDate = cus.TargetDate ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                              AND ware.CKEY = '{0}' ",CKEY);
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                        ) B ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                   ),0) AS 日实收金额,");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                TargetDate 日 ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("            FROM  dbo.CustomerBase cus ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("            WHERE  YEAR(TargetDate) = YEAR(GETDATE()) ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                AND MONTH(TargetDate) = MONTH(GETDATE()) ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("            GROUP BY TargetDate ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("           ) AS tbB ON CONVERT(CHAR(8),112) = tbB.日 ");
    #endregion
  }
  else if (type == "1")
  {
    #region 周
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat(" WITH  WeekDate ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("       AS ( SELECT  DATEADD(wk,DATEDIFF(wk,GETDATE()),0) AS riqi ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("         UNION ALL ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("         SELECT  riqi + 1 FROM   WeekDate ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("         WHERE  riqi + 1 <= ( SELECT  DATEADD(wk,6) ) ");
    strSql.AppendFormat("        ) ");
    strSql.AppendFormat("    SELECT CONVERT(CHAR(8),");
    strSql.AppendFormat("        DATENAME(weekday,CONVERT(CHAR(8),112)) DDay,");
    strSql.AppendFormat("        ISNULL(tbB.日成交量,");
    strSql.AppendFormat("        CASE WHEN CONVERT(CHAR(8),112) ");
    strSql.AppendFormat("          THEN NULL ");
    strSql.AppendFormat("          WHEN CONVERT(CHAR(8),112) ");
    strSql.AppendFormat("          THEN ISNULL(tbB.日成交量,0) ");
    strSql.AppendFormat("        END AS 日成交数量,");
    strSql.AppendFormat("        tbB.日实收金额,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 ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                        AND TargetDate = cus.TargetDate ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                  ) 日成交量,");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                  ISNULL(( SELECT SUM(Total) ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                      FROM  ( SELECT  SUM(CONVERT(FLOAT,0))) AS Total ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                           FROM   PaymentContent AS pay ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                           WHERE   PayDate = cus.TargetDate ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                                AND pay.CKEY = '{0}'",CKEY);
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                           UNION ALL ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                           SELECT  SUM(CONVERT(FLOAT,0))) AS Total ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                           FROM   dbo.CardRecharge8 AS recharge ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                           WHERE   RechargDate = cus.TargetDate ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                                AND recharge.CKEY = '{0}'",0))) AS Total ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                           FROM   dbo.PaymentSwimming AS payswim ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                           WHERE   PayDate = cus.TargetDate ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                                AND payswim.CKEY = '{0}'",0))) AS Total ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                           FROM   WarePaymentContent AS ware ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                           WHERE   PayDate = cus.TargetDate ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                                AND ware.CKEY = '{0}'",CKEY);
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                          ) B ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                     ),");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                  TargetDate 日 ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("              FROM  dbo.CustomerBase cus ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("              WHERE  DATEPART(wk,TargetDate) = DATEPART(wk,GETDATE()) ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                  AND DATEPART(yy,TargetDate) = DATEPART(yy,GETDATE()) ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("              GROUP BY TargetDate ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("             ) AS tbB ON CONVERT(CHAR(8),112) = tbB.日 ");
    #endregion
  }
  else if (type == "2")
  {
    #region 月

    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("SELECT YearMonth.月,");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("    tb.月成交量,");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("    CASE WHEN YearMonth.月 > MONTH(GETDATE()) THEN NULL ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.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 ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.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 ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                    AND MONTH(TargetDate) = MONTH(cus.TargetDate) ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("              ) 月成交量,");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("              ISNULL(( SELECT SUM(Total) ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                  FROM  ( SELECT  SUM(CONVERT(FLOAT,0))) AS Total ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                       FROM   PaymentContent AS pay ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                       WHERE   MONTH(PayDate) = MONTH(cus.TargetDate) ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                            AND pay.CKEY = '{0}' ",CKEY);
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                       UNION ALL ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                       SELECT  SUM(CONVERT(FLOAT,0))) AS Total ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                       FROM   dbo.CardRecharge8 AS recharge ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                       WHERE   MONTH(RechargDate) = MONTH(cus.TargetDate) ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                            AND recharge.CKEY = '{0}' ",0))) AS Total ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                       FROM   dbo.PaymentSwimming AS payswim ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                       WHERE   MONTH(PayDate) = MONTH(cus.TargetDate) ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                            AND payswim.CKEY = '{0}' ",0))) AS Total ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                       FROM   WarePaymentContent AS ware ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                       WHERE   MONTH(PayDate) = MONTH(cus.TargetDate) ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                            AND ware.CKEY = '{0}' ",CKEY);
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                      ) B ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                 ),0) AS 月实收总金额,");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("              MONTH(TargetDate) 月 ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("          FROM  dbo.CustomerBase cus ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("          WHERE  YEAR(TargetDate) = YEAR(GETDATE()) ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("          GROUP BY MONTH(cus.TargetDate) ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("         ) AS tb ON YearMonth.月 = tb.月 ");
    #endregion
  }
  else if (type == "3")
  {
    #region 年
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("SELECT ( SELECT  COUNT(1) ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("       FROM   dbo.CustomerBase base ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("       WHERE   CKEY = '{0}' ",CKEY);
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("            AND " + impomo.TotalConsumptionMon + " > 0 ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("            AND YEAR(TargetDate) = YEAR(cus.TargetDate) ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("      ) 年成交量,");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("      CONVERT(NVARCHAR(20),CONVERT(DECIMAL(18,2),ISNULL(( SELECT SUM(Total) ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("          FROM  ( SELECT  SUM(CONVERT(FLOAT,0))) AS Total ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("               FROM   PaymentContent AS pay ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("               WHERE   YEAR(PayDate) = YEAR(cus.TargetDate) ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                    AND pay.CKEY = '{0}' ",CKEY);
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("               UNION ALL ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("               SELECT  SUM(CONVERT(FLOAT,0))) AS Total ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("               FROM   dbo.CardRecharge8 AS recharge ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("               WHERE   YEAR(RechargDate) = YEAR(cus.TargetDate) ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                    AND recharge.CKEY = '{0}' ",0))) AS Total ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("               FROM   dbo.PaymentSwimming AS payswim ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("               WHERE   YEAR(PayDate) = YEAR(cus.TargetDate) ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                    AND payswim.CKEY = '{0}' ",0))) AS Total ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("               FROM   WarePaymentContent AS ware ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("               WHERE   YEAR(PayDate) = YEAR(cus.TargetDate) ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("                    AND ware.CKEY = '{0}' ",CKEY);
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("              ) B ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("         ),0))) AS 年实收总金额,");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("      YEAR(TargetDate) 年 ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("  FROM  dbo.CustomerBase cus ");
    str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.AppendFormat("  GROUP BY YEAR(TargetDate) ");
    #endregion
  }

  #endregion

  DataTable table = DBHelper.GetDateTable(str<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>.ToString());
  string rs = Newtonsoft.Json.JsonConvert.SerializeObject(table);
  return rs;
}</pre>
原文链接:https://www.f2er.com/mssql/63373.html

猜你在找的MsSQL相关文章