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

最新文章

  1. JAVASE02-Unit03: 日期操作 、 集合框架
  2. [css]【转载张鑫旭】我是如何对网站CSS进行架构的
  3. 使用ttXactAdmin、ttSQLCmdCacheInfo、ttSQLCmdQueryPlan获取SQL相关具体信息[TimesTen运维]
  4. 【HDOJ】3592 World Exhibition
  5. HTML5 History对象,Javascript修改地址栏而不刷新页面
  6. oracle开机自启动-超简单
  7. 特性(Attributes)
  8. $.extend 用法
  9. JavaScript深入浅出补充——(二)语句和严格模式,对象
  10. 翻译 | Thingking in Redux(如果你只了解MVC)
  11. [Count the numbers satisfying (m + sum(m) + sum(sum(m))) equals to N]
  12. 日历插件——laydate.js
  13. CountDownLatch使用
  14. appniu踩坑
  15. 局域网内Ping不通
  16. Tencent Cloud Developers Conference(2018.12.15)
  17. 【python小练】0020
  18. centos7配置固定ip
  19. strip函数
  20. Facade 设计模式

热门文章

  1. oracle初次使用连接不上
  2. sqlite加密
  3. samba服务器源码安装(非rpm)
  4. StringBuffer类的方法
  5. [C++]C++标准里 string和wstring
  6. 周赛-Toy Cars 分类: 比赛 2015-08-08 15:41 5人阅读 评论(0) 收藏
  7. Tiling 分类: POJ 2015-06-17 15:15 8人阅读 评论(0) 收藏
  8. css基本样式总结
  9. 2016年省赛G题, Parenthesis
  10. css3 resize box-sizing outline-offset