SQL Povit
2024-10-11 21:43:59
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
最新文章
- JAVASE02-Unit03: 日期操作 、 集合框架
- [css]【转载张鑫旭】我是如何对网站CSS进行架构的
- 使用ttXactAdmin、ttSQLCmdCacheInfo、ttSQLCmdQueryPlan获取SQL相关具体信息[TimesTen运维]
- 【HDOJ】3592 World Exhibition
- HTML5 History对象,Javascript修改地址栏而不刷新页面
- oracle开机自启动-超简单
- 特性(Attributes)
- $.extend 用法
- JavaScript深入浅出补充——(二)语句和严格模式,对象
- 翻译 | Thingking in Redux(如果你只了解MVC)
- [Count the numbers satisfying (m + sum(m) + sum(sum(m))) equals to N]
- 日历插件——laydate.js
- CountDownLatch使用
- appniu踩坑
- 局域网内Ping不通
- Tencent Cloud Developers Conference(2018.12.15)
- 【python小练】0020
- centos7配置固定ip
- strip函数
- Facade 设计模式
热门文章
- oracle初次使用连接不上
- sqlite加密
- samba服务器源码安装(非rpm)
- StringBuffer类的方法
- [C++]C++标准里 string和wstring
- 周赛-Toy Cars 分类: 比赛 2015-08-08 15:41 5人阅读 评论(0) 收藏
- Tiling 分类: POJ 2015-06-17 15:15 8人阅读 评论(0) 收藏
- css基本样式总结
- 2016年省赛G题, Parenthesis
- css3 resize box-sizing outline-offset