行列转换
2016-03-10 11:32
316 查看
CREATE proc [dbo].[R_Parities_Config_CurrencyExchangeRate_V2]
@FromCode nvarchar(400),
@ToCode nvarchar(400),
@EffectiveDate nvarchar(400),
@ChangeDate nvarchar(400)
as
begin
SET NOCOUNT ON;
select f.col as FromCode into #FromTable from (select distinct col from dbo.[Split](@FromCode, ',')) as f
where f.col in (select distinct FromCode from Config_CurrencyExchangeRate where ToCode = @ToCode and FromCode != @ToCode)
select * into #ToTable from Config_CurrencyExchangeRate
where ToCode = @ToCode and EffectiveDate >=@EffectiveDate and EffectiveDate <=@ChangeDate
select a.EffectiveDate,c.FromCode+'/'+a.ToCode as CurrencyType,a.Rate into #a from #ToTable as a
right join #FromTable as c on c.FromCode = a.FromCode order by a.EffectiveDate desc
declare @groupField varchar(1000)
select @groupField=isnull(@groupField,'')+case when isnull(@groupField,'')='' then '' else ',' end+QUOTENAME(CurrencyType)
from (select rtrim(CurrencyType) as CurrencyType from (select distinct CurrencyType from #a) as a )t
declare @sql nvarchar(4000)
set @sql=N'
select *
from
(select EffectiveDate,rtrim(CurrencyType) as CurrencyType,
MAX(Rate) as Rate
from #a
group by EffectiveDate,rtrim(CurrencyType)
) as x
pivot (MAX(Rate)for CurrencyType in ('+@groupField+')) as pvt
order by EffectiveDate desc'
EXEC (@sql)
drop table #a
end
--execute [R_Parities_Config_CurrencyExchangeRate_V2] 'CAD,USD','USD','2010-12-12','2016-12-12'
--select distinct ToCode,FromCode from Config_CurrencyExchangeRate
GO
@FromCode nvarchar(400),
@ToCode nvarchar(400),
@EffectiveDate nvarchar(400),
@ChangeDate nvarchar(400)
as
begin
SET NOCOUNT ON;
select f.col as FromCode into #FromTable from (select distinct col from dbo.[Split](@FromCode, ',')) as f
where f.col in (select distinct FromCode from Config_CurrencyExchangeRate where ToCode = @ToCode and FromCode != @ToCode)
select * into #ToTable from Config_CurrencyExchangeRate
where ToCode = @ToCode and EffectiveDate >=@EffectiveDate and EffectiveDate <=@ChangeDate
select a.EffectiveDate,c.FromCode+'/'+a.ToCode as CurrencyType,a.Rate into #a from #ToTable as a
right join #FromTable as c on c.FromCode = a.FromCode order by a.EffectiveDate desc
declare @groupField varchar(1000)
select @groupField=isnull(@groupField,'')+case when isnull(@groupField,'')='' then '' else ',' end+QUOTENAME(CurrencyType)
from (select rtrim(CurrencyType) as CurrencyType from (select distinct CurrencyType from #a) as a )t
declare @sql nvarchar(4000)
set @sql=N'
select *
from
(select EffectiveDate,rtrim(CurrencyType) as CurrencyType,
MAX(Rate) as Rate
from #a
group by EffectiveDate,rtrim(CurrencyType)
) as x
pivot (MAX(Rate)for CurrencyType in ('+@groupField+')) as pvt
order by EffectiveDate desc'
EXEC (@sql)
drop table #a
end
--execute [R_Parities_Config_CurrencyExchangeRate_V2] 'CAD,USD','USD','2010-12-12','2016-12-12'
--select distinct ToCode,FromCode from Config_CurrencyExchangeRate
GO
相关文章推荐
- tomcat集成activeMq 简单例子
- scp lost connection
- 设计模式_单例模式
- 如何做一个价值百亿的O2O创业项目
- cf B. Brand New Easy Problem
- iOS Client 与WebSocket 通信(一)
- Javascript日期对比
- 互联网时代中生存
- Class.forName()用法详解
- [PHP]GD库生成图像无法在浏览器正常显示的问题(更新:使用GD图像生成类发现错误必须及时清理缓存!)
- 学习进度更新记录
- 泛型
- 使用php simple html dom parser解析html标签
- linux awk命令详解
- BSTR、char*和CString转换
- 解决Word2013光标乱跳的问题
- Android中实时视频传输(摄像头实时视频传输)解决方案<二>
- WCF 入门教程
- IT公司汇总(网络版)
- HBase快速导入数据--BulkLoad