一个非常常见的报表,分析会员组成比例 以及最新增长情况 和上月同期会员增长情况。
比较特殊一点的是 报表中的 普通会员 和 金卡会员 临时会员 银卡会员 等列 都是根据会员等级配置表动态生成的(即我是不知道会有多少个vip等级的)。
 
sql如下(有点挫的感觉 应该会有更优雅的方式实现):
 
select  N=1, AgentID, AgentBigArea,  AgentName,CusALevel,CusCreateDate into #temp from Customer inner join CusAccount on  
CusAVIPNo = VIPNo inner join Agent on AgentID = CusAgentID where CusStatus = 1 and CusCreateDate<'2016-03-31 23:59:59'

--插入区域小计
insert into #temp select SUM(N),-1,AgentBigArea+'_小计',AgentBigArea+'_小计',CusALevel,'' from #temp group by AgentBigArea,CusALevel

--插入总计
insert into #temp select SUM(N),-1,'总合计','总合计',CusALevel,'' from #temp where AgentBigArea not like '%小计%' group by CusALevel
 
declare @sql nvarchar(max)

 
-- 空格这么多是因为不能设置字符串长度  很挫 暂时这样处理吧
set @sql = 'select ''                                                                             '' as ''增长率'', Agentname as ''分销商'',agentid,AgentBigArea as ''区域'', SUM(case when CusCreateDate>''2016-03-01'' and CusCreateDate<''2016-03-31'' then N else 0 end) as ''本期'' ,
SUM(case when CusCreateDate>Dateadd(MONTH, -1, ''2016/3/1 0:00:00'') and CusCreateDate<Dateadd(MONTH, -1, ''2016/3/31 0:00:00'') then N else 0 end) as ''上期'',
SUM(N) as ''总会员数'''

--动态行转列
SELECT @sql = @sql + ',SUM(case CusALevel when ' +CAST(CusLevelID as nvarchar(10))+' then N else 0 end) as [' +CusLevelName+']'    
FROM
  (SELECT TOP 10000 CusLevelID,CusLevelName                  
   FROM CusLevel  
    ) AS a
print @sql
 SET @sql = @sql + ' into ##temp1 from #temp group by agentname,agentid,AgentBigArea order by AgentBigArea '
 
 EXEC (@sql)
 

--个人觉得比较挫的地方 用到了全局临时表
 select * into #temp2 from ##temp1
 drop table ##temp1  
 
 --计算区域小计行的本期和上期
 update #temp2 set 本期=bq,上期=sq from #temp2 inner join (select 区域+'_小计' as AgentBigArea,  SUM(上期) as sq,SUM(本期) as bq from #temp2
 where 区域 not like '%小计%' group by 区域 ) as t on t.AgentBigArea = #temp2.区域  where #temp2.区域 like '%小计%'
 
 --计算总合计的本期和上期
 update #temp2 set 本期=(select SUM(本期) from #temp2 where 区域 like '%小计%' ),上期=(select SUM(上期) from #temp2 where 区域 like '%小计%' ) where 区域 = '总合计'
 
  --计算增长率
  update #temp2 set 增长率 = (case 上期 when 0 then '' else convert(varchar(30),cast((本期-上期)/convert(decimal(10,2),上期) *100 as decimal(18,2)) )+'%' end)  from #temp2
 
  select * from #temp2

 
 
 

vip会员统计表 (vip等级是灵活配置的 非写死1是金卡用户 2是什么 等)的更多相关文章

  1. 游戏中VIP会员模块的简单实现

    哈哈  今天周末有时间,再整理一篇博文上来,虽然已经不做游戏老长时间了,但还是要把以前做过的东西总结一下,借此可以回顾以前的东西,也可以分享给大家. 今天说一下游戏中VIP会员模块的实现思路.每款游戏 ...

  2. 限制只有VIP会员才能下载Ecshop文章页的附件

    以官方2.7.2默认模板为基础来讲述一下"如何在文章详情页限制只有VIP会员才能下载相关附件"这里假设VIP会员的等级ID为2首先修改 article.php 文件打开 /arti ...

  3. 不开vip会员照样看vip电影(亲测有效)

    此为临时链接,仅用于文章预览,将在短期内失效关闭 不开vip会员照样看vip电影(亲测有效) 2018-03-08 mr_lee Python达人课堂 刚刚测试,真实有效,颇不接待要分享了... 土豪 ...

  4. 破解VIP会员视频集合

    浏览器安装暴力猴扩展即可使用 // ==UserScript== // @name 破解VIP会员视频集合 // @namespace https://greasyfork.org/zh-CN/use ...

  5. 参数(条件表)灵活配置GS01/GS02/GS03

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  6. oracle 11g rac 修改VIP、scan VIP、priv IP

    11GR2 RAC modify vip,public ip,private ip,scan vip实施步骤1 修改目的    根据业务的需求,需要由原来的临时IP改为生产ip,以下为调整前后对应的I ...

  7. 【FICO系列】SAP 参数(条件表)灵活配置GS01/GS02/GS03

      公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[FICO系列]SAP 参数(条件表)灵活配 ...

  8. 前端快闪三:多环境灵活配置react

    你已经使用Create React App脚手架搭建了React应用,现在该部署了. 一般会使用npm run build或者yarn build构建出静态资源, 由web服务器承载. 您会体验到 多 ...

  9. MongoDB ReplacaSet & Sharding集群安装 配置 和 非集群情况的安装 配置 -摘自网络

    单台机器做sharding --单机配置集群服务(Sharding) --shard1_1 mongod --install --serviceName MongoDBServerShard1 --s ...

随机推荐

  1. error "Can only specify query options (orderby, where, take, skip) after last navigation" when fetching a List<string>

    Question I use OData v3 and WCF in visual studio 2012. I want to return List<string> using the ...

  2. 剑指offer三十之连续子数组的最大和

    一.题目 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是,如果向量 ...

  3. (转)python高级:列表解析和生成表达式

    一.语法糖的概念 “糖”,可以理解为简单.简洁,“语法糖”使我们可以更加简洁.快速的实现这些功能. 只是Python解释器会把这些特定格式的语法翻译成原本那样复杂的代码逻辑 我们使用的语法糖有: if ...

  4. (转)windows 2003 远程桌面关闭 运行程序退出解决

    windows 2003 远程桌面关闭 运行程序退出解决 原文:http://2798996.blog.51cto.com/2788996/503365 情况:远程桌面到2003,运行一个程序,然后关 ...

  5. 【C#小知识】C#中一些易混淆概念总结(八)---------解析接口 分类: C# 2014-02-18 00:09 2336人阅读 评论(4) 收藏

     这一篇主要来解析关于面向对象中最总要的一个概念--接口. 对于接口来说,C#是有规定使用Interface关键字来声明接口.它的声明是和类一致的.可以说接口就是一个特殊的抽象类.如下代码: cl ...

  6. 【字符串】Reverse Words in a String(两个栈)

    题目: Given an input string, reverse the string word by word. For example,Given s = "the sky is b ...

  7. Java的符号扩展与零扩展

    byte b = -127; System.out.println(b); // -127 int b1 = b & 0xff; System.out.println(b1); // 129 ...

  8. Docker挂载主机目录Docker访问出现Permission denied的解决办法

    Docker挂载主机目录,访问相应的文件出现Premission denied的权限访问问题,   [root@localhost soft]# docker images REPOSITORY    ...

  9. Linux命令-用户、用户组、权限

    参考资料: http://www.linuxidc.com/Linux/2014-07/104445.htm    Linux入门教程:如何手动创建一个Linux用户 http://www.linux ...

  10. freepbx对接gms网关

    前面的文章阿里云使用镜像安装freepbx , 安装freepbx后创建sip分机 ,freepbx的SIP通话客户端X-lite Yate eyeBeam Linphone我们已经成功的创建好了分机 ...