SQL Povit
declare @yr int,@mth int,@logistics_code varchar(30),@fee_type varchar(50) select @yr=2014,@mth=1,@logistics_code='All',@fee_type='All' declare @index int,@from_date datetime,@to_date datetime,@strDateList varchar(180),@strSql varchar(1000)
select @index=1,@strDateList='',@from_date=cast((ltrim(@yr)+'-'+ltrim(@mth)+'-1') as datetime)
set @to_date=@from_date
while @index<=12
begin
set @strDateList=@strDateList+'['+ltrim(year(@to_date))+'/'+ltrim(month(@to_date))+'],'
set @to_date=dateadd(mm,1,@to_date)
set @index=@index+1
end create table #tbl
(
ymth varchar(20),
logistics_code varchar(30),
fee money
) insert into #tbl(ymth,logistics_code,fee)
select ltrim(yr)+'/'+ltrim(mth),logistics_code,sum(fee)
from tbl_lis_FeeList where status='A'
and
(
(yr=year(@from_date) and mth>=month(@from_date))
or (yr=year(@to_date) and mth<=month(@to_date))
)
and logistics_code=case when @logistics_code<>'All' then @logistics_code else logistics_code end
and fee_type=case when @fee_type<>'All' then @fee_type else fee_type end
group by ltrim(yr)+'/'+ltrim(mth),logistics_code set @strDateList=substring(@strDateList,1,len(@strDateList)-1)
set @strSql='select b.customer_name,'+@strDateList+' from
(
select logistics_code,'+@strDateList+' from #tbl pivot(sum(fee) for ymth in('+@strDateList+')) as t
) as temp
left join tbl_lis_LogisticsProvider b on temp.logistics_code=b.logistics_code' exec (@strSql) /*
select customer_name,temp.* from (
select logistics_code,[2014/1],[2014/2],[2014/3],[2014/4],[2014/5],[2014/6],[2014/7],[2014/8],[2014/9],[2014/10],[2014/11],[2014/12]
from #tbl
pivot(sum(fee) for ymth in([2014/1],[2014/2],[2014/3],[2014/4],[2014/5],[2014/6],[2014/7],[2014/8],[2014/9],[2014/10],[2014/11],[2014/12])) as t
) as temp
left join tbl_lis_LogisticsProvider b on temp.logistics_code=b.logistics_code
*/ drop table #tbl
GO

SQL Povit的更多相关文章
- 每日学习心得:SQL查询表的行列转换/小计/统计(with rollup,with cube,pivot解析)
2013-8-20 1. SQL查询表的行列转换/小计/统计(with rollup,with cube,pivot解析) 在实际的项目开发中有很多项目都会有报表模块,今天就通过一个小的SQL ...
- Sql Server 列转行 Pivot使用
今天正好做 数据展示,用到了列转行,行转列有多种方式,Pivot是其中的一种,Povit 是sql server 2005以后才出现的功能, 下面的业务场景: 每个月,进货渠道的总计数量[Total] ...
- SQL点滴19—T-SQL中的透视和逆透视
原文:SQL点滴19-T-SQL中的透视和逆透视 透视 今天抽一点时间来看看透视和逆透视语句,简单的说就是行列转换.假设一个销售表中存放着产品号,产品折扣,产品价格三个列,每一种产品号可能有多种折扣, ...
- SQL查询表的行列转换/小计/统计(with rollup,with cube,pivot解析)
SQL查询表的行列转换/小计/统计(with rollup,with cube,pivot解析) 2013-8-20 1. SQL查询表的行列转换/小计/统计(with rollup,with ...
- SQL Server 行列相互转换命令:PIVOT和UNPIVOT使用详解
一.使用PIVOT和UNPIVOT命令的SQL Server版本要求 1.数据库的最低版本要求为SQL Server 2005 或更高. 2.必须将数据库的兼容级别设置为90 或更高. 3.查看我的数 ...
- PIVOT 和 UNPIVOT 命令的SQL Server版本
I:使用 PIVOT 和 UNPIVOT 命令的SQL Server版本要求 1.数据库的最低版本要求为 SQL Server 2005 或 更高 2.必须将数据库的兼容级别设置为 90 或 更高 3 ...
- sql语句实现行转列的3种方法实例
sql语句实现行转列的3种方法实例 一般在做数据统计的时候会用到行转列,假如要统计学生的成绩,数据库里查询出来的会是这样的,但这并不能达到想要的效果,所以要在查询的时候做一下处理,下面话不多说了,来一 ...
- 最近帮客户实施的基于SQL Server AlwaysOn跨机房切换项目
最近帮客户实施的基于SQL Server AlwaysOn跨机房切换项目 最近一个来自重庆的客户找到走起君,客户的业务是做移动互联网支付,是微信支付收单渠道合作伙伴,数据库里存储的是支付流水和交易流水 ...
- SQL Server 大数据搬迁之文件组备份还原实战
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 解决方案(Solution) 搬迁步骤(Procedure) 搬迁脚本(SQL Codes) ...
随机推荐
- Git 使用规范流程
Git教程:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 团队开发中,遵循一个合 ...
- git 用法
git init #创建仓库git add _filename #添加文件到缓冲区git commit -m "msg" #提交更新,从缓冲区提交到版本库git status #查 ...
- Java编译那些事儿【转】
转自:http://blog.csdn.net/lincyang/article/details/8553481 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 命令行编译 使 ...
- [转]vs2008安装失败的总结与分享
转自:http://www.cnblogs.com/rockdean/archive/2010/01/13/1646851.html 今天系统是刚装的,今儿个也不是第一次装系统,也不是第一次装vs20 ...
- PHP中Redis替代文件存储Session语句
php默认使用文件存储session,如果并发量大,效率非常低.而Redis对高并发的支持非常好,所以,可以使用redis替代文件存储session. 这里,介绍下php的 session_set_s ...
- linux新内核的时钟机制代码
http://blog.chinaunix.net/uid-22810130-id-384173.html 如果说cfs是linux的一个很有创意的机制的话,那么linux中另一个创意就是nohz,我 ...
- c#danliemosih
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 打印机 ...
- c#之线程池优先级
using System; using System.Threading; namespace ConsoleApplication1 { class Program { static void Ma ...
- C#之桶中取黑白球问题
<编程之美>284页,问题4.6:桶中取黑白球. 有一个桶,里面有白球.黑球各100个,人们必须按照以下规则把球取出来: 1. 每次从桶中拿两个球: 2. 如果两球同色,再放入一个黑球: ...
- jquery简单插件到复杂插件(2)--简单手风琴
手风琴就是展示与隐藏 <div id="dataContent"> <div class="dataLeft fl"> <div ...