行列转换一

--试..
if exists(select 1 from sysobjects where id=object_id('f02')) drop Table f02
go
create table f02(yDate smalldatetime,yIndex varchar(10),yVal dec(20,10))
insert into f02
select '2006-9-8 01:33:34','1','234.52578' union all select
'2006-9-8 01:33:34','2','352.86792' union all select
'2006-9-8 01:33:34','3','.875628456' union all select
'2006-9-8 02:34:35','1','252.67984' union all select
'2006-9-8 02:34:35','2','287.86874' union all select
'2006-9-9 02:34:35','3','.875623576'
go

if exists(select 1 from sysobjects where id=object_id('p_InsertTable')) drop Proc p_InsertTable
go

Create Proc p_InsertTable
@getdate datetime=null
as
declare @s varchar(8000),@date varchar(10),@date2 varchar(10)
set @getdate =isnull(@getdate,getdate())
set @date = convert(char(8),@getdate,112)
set @date2 = convert(char(8),@getdate+1,112)
set @s='select 时间=datename(hour,yDate)'
select @s=@s+',流量'+Rtrim(yIndex)+'=sum(case when yIndex='''+yIndex+''' then yVal else 0 end)' from f02 group by yIndex
set @s=@s+' from f02 where yDate >= '''+ @date +''' and yDate < '''+ @date2 +''' group by datename(hour,yDate)'
exec(@s)
go
--
exec dbo.p_InsertTable
exec dbo.p_InsertTable '20060909'
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值