这种情况如何写存储过程?(表中纵向列的数据,要横放过来)

mujin 2005-08-31 04:30:43
用SELECT语句,可以得出以下数据
desc dotime date
浓度 8:00 2.3
浓度 9:00 2.5
浓度 10:00 2.6
浓度 11:00 2.1
PH值 8:00 2
PH值 9:00 3.6
PH值 10:00 5
PH值 11:00 6
...
——
现在希望通过在存储过程建立临时表,临时表为
desc date1 date2 date3 date4 ... date12 ave max min 极差
浓度 2.3 2.5 2.6 2.1
PH值 2 3.6 5 6

注:其中date数据个数不一定,但希望临时表做到date数据个数最多为16个,超过就换页,不超过12个,后面接平均值、最大值、最小值和极差。超过12个,则平均值等放到后面一页。
不知道怎么写这个存储过程好!谢谢帮忙的朋友!
...全文
206 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2005-09-01
  • 打赏
  • 举报
回复
用我的方法,一条记录最多显示的data数目可以通过存储过程中的@cols调整
zjcxc 元老 2005-09-01
  • 打赏
  • 举报
回复
--生成测试数据
create table tb([desc] varchar(10),dotime varchar(20),data numeric(5,1))
insert into tb select '浓度','08:00',2.3
insert into tb select '浓度','09:00',2.5
insert into tb select '浓度','10:00',2.6
insert into tb select '浓度','11:00',2.1
insert into tb select 'PH值','08:00',2
insert into tb select 'PH值','09:00',3.6
insert into tb select 'PH值','10:00',5
insert into tb select 'PH值','11:00',6
insert into tb select 'PH值','12:00',2
insert into tb select 'PH值','13:00',3.6
insert into tb select 'PH值','14:00',5
insert into tb select 'PH值','15:00',6
insert into tb select 'PH值','16:00',2
insert into tb select 'PH值','17:00',3.6
insert into tb select 'PH值','18:00',5
insert into tb select 'PH值','19:00',6
insert into tb select 'PH值','20:00',2
insert into tb select 'PH值','21:00',3.6
insert into tb select 'PH值','22:00',5
insert into tb select 'PH值','23:00',66
insert into tb select 'PH值','00:00',666
go

--创建存储过程
create procedure p_test
as
set nocount on
declare @cols int,@s varchar(8000),@i varchar(10),@b varchar(8000)
select top 1
@cols=12, --一条记录最多12列
@s='',
@i=case when count(*)>@cols then @cols-1 else count(*)-1 end,
@b=',
[avg]=case when (a.id-b.id)/'+rtrim(@cols)+'=b.gid then cast(b.[avg] as varchar) else '''' end,
[max]=case when (a.id-b.id)/'+rtrim(@cols)+'=b.gid then cast(b.[max] as varchar) else '''' end,
[min]=case when (a.id-b.id)/'+rtrim(@cols)+'=b.gid then cast(b.[min] as varchar) else '''' end,
[极差]=case when (a.id-b.id)/'+rtrim(@cols)+'=b.gid then cast(b.[max]-b.[min] as varchar) else '''' end
from # a
left join(
select [desc],
[avg]=cast(avg(cast(data as numeric(5,1))) as decimal(5,1)),
[max]=cast(max(cast(data as numeric(5,1))) as decimal(5,1)),
[min]=cast(min(cast(data as numeric(5,1))) as decimal(5,1)),
gid=(max(id)-min(id))/'+rtrim(@cols)+',
id=min(id)
from #
group by [desc]
)b on a.[desc]=b.[desc]
-- and (a.id-b.id)/'+rtrim(@cols)+'=b.gid
group by a.[desc],(a.id-b.id)/'+rtrim(@cols)+',b.gid,b.[avg],b.[max],b.[min]'
from tb
group by [desc]
order by count(*) desc
while @i>=0
select @s=',[data'+rtrim(@i+1)+']=max(case (a.id-b.id)%'+rtrim(@cols)+' when '+@i+' then data else '''' end)'+@s,
@i=@i-1

exec('
select id=identity(int,1,1),[desc],dotime,data=cast(data as varchar)
into #
from tb
order by [desc]--,dotime

select a.[desc]'+@s+@b)

go

--执行查询
exec p_test
go

drop proc p_test
drop table tb

/*--输出结果
desc data1 data2 data3 data4 data5 data6 data7 data8 data9 data10 data11 data12 avg max min 极差
---------- ------ ----- ----- ----- ----- ----- ----- ----- ----- ------- ------ ------ ----- ----- ----- ----
PH值 2.0 3.6 5.0 6.0 2.0 3.6 5.0 6.0 2.0 3.6 5.0 6.0
PH值 2.0 3.6 5.0 66.0 666.0 46.6 666.0 2.0 664.0
浓度 2.3 2.5 2.6 2.1 2.4 2.6 2.1 0.5
--*/
mujin 2005-09-01
  • 打赏
  • 举报
回复
谢谢libin_ftsafe(子陌红尘) !
这样做出来在12个数据内,是没问题的,超过12个,后面的平均值等都没了,超过16个的数据,也都不显示出来了。我的意思是希望超过12个数据,则后面的数据及平均值等放到下一页去,我再试一下!
非常谢谢子陌红尘!^-^
mujin 2005-09-01
  • 打赏
  • 举报
回复
谢谢zjcxc(邹建)!非常感谢!^-^
子陌红尘 2005-08-31
  • 打赏
  • 举报
回复
--生成测试数据
create table #t([desc] varchar(10),dotime varchar(20),data numeric(5,1))
insert into #t select '浓度','08:00',2.3
insert into #t select '浓度','09:00',2.5
insert into #t select '浓度','10:00',2.6
insert into #t select '浓度','11:00',2.1
insert into #t select 'PH值','08:00',2
insert into #t select 'PH值','09:00',3.6
insert into #t select 'PH值','10:00',5
insert into #t select 'PH值','11:00',6


--创建存储过程
create procedure sp_test
as
begin
declare @s varchar(8000)
declare @i int
set @s = ''
set @i = 0

set rowcount 16
select
@i = @i + 1,
@s = @s + ',[data'+rtrim(@i)+']=max(case dotime when '''
+ dotime + ''' then data end)'
from
#t
group by
dotime
set rowcount 0

if @i <= 12
set @s = @s+',[avg]=avg(data),[max]=max(data)'
+',[min]=min(data),极差=max(data)-min(data)'

set @s = 'select [desc]'+@s+' from #t group by [desc]'
exec(@s)
end
go


--执行查询
exec sp_test


--输出结果
desc data1 data2 data3 data4 avg max min 极差
------ ----- ----- ----- ----- ----- ----- ----- -----
PH值 2.0 3.6 5.0 6.0 4.150 6.0 2.0 4.0
浓度 2.3 2.5 2.6 2.1 2.375 2.6 2.1 0.5
子陌红尘 2005-08-31
  • 打赏
  • 举报
回复
--生成测试数据
create table #t([desc] varchar(10),dotime varchar(20),data numeric(5,1))
insert into #t select '浓度','08:00',2.3
insert into #t select '浓度','09:00',2.5
insert into #t select '浓度','10:00',2.6
insert into #t select '浓度','11:00',2.1
insert into #t select 'PH值','08:00',2
insert into #t select 'PH值','09:00',3.6
insert into #t select 'PH值','10:00',5
insert into #t select 'PH值','11:00',6


--执行查询
declare @s varchar(8000)
declare @i int
set @s = ''
set @i = 0

set rowcount 16
select
@i = @i + 1,
@s = @s + ',[data'+rtrim(@i)+']=max(case dotime when '''+dotime+''' then data end)'
from
#t
group by
dotime
set rowcount 0

if @i <= 12
set @s = @s+',[avg]=avg(data),[max]=max(data),[min]=min(data),极差=max(data)-min(data)'

set @s = 'select [desc]'+@s+' from #t group by [desc]'
exec(@s)


--输出结果
desc data1 data2 data3 data4 avg max min 极差
------ ----- ----- ----- ----- ----- ----- ----- -----
PH值 2.0 3.6 5.0 6.0 4.150 6.0 2.0 4.0
浓度 2.3 2.5 2.6 2.1 2.375 2.6 2.1 0.5
zjcxc 元老 2005-08-31
  • 打赏
  • 举报
回复
表中有主键吗?
mujin 2005-08-31
  • 打赏
  • 举报
回复
谢谢 postfxj(探索者) ,还是发个例子给我吧,TKS!偶下班了,明天在来研究。
mujin 2005-08-31
  • 打赏
  • 举报
回复
就是这个意思,因为desc列是固定的,比如就是浓度、PH值。我要做到这样:

desc date1 date2 date3 date4 ... date12 date13 date14 date15 date16
浓度 2.3 2.5 2.6 2.1 ... 2.3 2.3 2.6 2.9 3
PH值 2 3.6 5 6 ... 5 4 5 1 2
浓度 3.3 3.2
PH值 5 4

所谓换页,是列中数据放到下一页。不知道存储过程能不能做到。

postfxj 2005-08-31
  • 打赏
  • 举报
回复
如還不明白,到時再發個例子給你
postfxj 2005-08-31
  • 打赏
  • 举报
回复
1 動態建立你的臨時表
2 把基本數5據插入臨時表
3 動態產生update語句,主要是在case 語句那一塊。

然後用 exec(@sql)
rivery 2005-08-31
  • 打赏
  • 举报
回复
对列进行换页?

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧