有一表结构如下
id index xm xm1 xm2 hz
8 3 wdy 5.0 5.0 10.00
9 3 hzs 5.0 5.0 12.00
10 3 xjb 4.0 6.0 15.00
16 4 wdy 5.0 5.0 11.00
17 4 hzs 5.0 5.0 114.00
18 4 xjb 5.0 5.0 20.00
19 5 wdy 4.0 6.0 12.00
20 5 hzs 5.0 5.0 18.00
21 5 xjb 4.0 6.0 19.00
=====================================================================
现在要实现如下查询结果
index wdy hzs xjb
3 10.00 12.00 15.00
4 11.00 114.00 20.00
5 12.00 18.00 19.00
===========================================================================
高手们 如何实现!!!
id index xm xm1 xm2 hz
8 3 wdy 5.0 5.0 10.00
9 3 hzs 5.0 5.0 12.00
10 3 xjb 4.0 6.0 15.00
16 4 wdy 5.0 5.0 11.00
17 4 hzs 5.0 5.0 114.00
18 4 xjb 5.0 5.0 20.00
19 5 wdy 4.0 6.0 12.00
20 5 hzs 5.0 5.0 18.00
21 5 xjb 4.0 6.0 19.00
=====================================================================
现在要实现如下查询结果
index wdy hzs xjb
3 10.00 12.00 15.00
4 11.00 114.00 20.00
5 12.00 18.00 19.00
===========================================================================
高手们 如何实现!!!
hzs=max(case when xm='hzs' then hz else 0 end),
xjb=max(case when xm='xjb' then hz else 0 end)
from tb group by index
-- Author :fredrickhu(小F 向高手学习)
-- Date :2009-08-12 14:48:03
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[index] int,[xm] varchar(3),[xm1] numeric(2,1),[xm2] numeric(2,1),[hz] numeric(5,2))
insert [tb]
select 8,3,'wdy',5.0,5.0,10.00 union all
select 9,3,'hzs',5.0,5.0,12.00 union all
select 10,3,'xjb',4.0,6.0,15.00 union all
select 16,4,'wdy',5.0,5.0,11.00 union all
select 17,4,'hzs',5.0,5.0,114.00 union all
select 18,4,'xjb',5.0,5.0,20.00 union all
select 19,5,'wdy',4.0,6.0,12.00 union all
select 20,5,'hzs',5.0,5.0,18.00 union all
select 21,5,'xjb',4.0,6.0,19.00
--------------开始查询--------------------------
declare @sql varchar(8000)
set @sql = 'select [index]'
select @sql = @sql + ' , sum(case xm when ''' + xm + ''' then hz else 0 end) [' + xm + ']'
from (select distinct xm from tb) as a
set @sql = @sql + ' from tb group by [index]'
exec(@sql)
----------------结果----------------------------
/*index hzs wdy xjb
----------- ---------------------------------------- ---------------------------------------- ----------------------------------------
3 12.00 10.00 15.00
4 114.00 11.00 20.00
5 18.00 12.00 19.00*/
[/code]
2楼正解但是index一定要[]因为是关键字 不然会报错的select [index],wdy=max(case when xm='wdy' then hz else 0 end),
hzs=max(case when xm='hzs' then hz else 0 end),
xjb=max(case when xm='xjb' then hz else 0 end)
from table_1 group by [index] case then效率更高
----------- ----------- -------------------------------------------------- ----------- ----------- -----------
1 3 wdy 5 5 10
2 3 hzs 5 5 12
3 3 xjb 4 6 15
4 4 wdy 5 5 11
5 4 hzs 5 5 114
6 4 xjb 5 5 20
7 5 wdy 4 6 12
8 5 hzs 5 5 18
9 5 xjb 4 6 19(9 行受影响)index wdy hzs xjb
----------- ----------- ----------- -----------
3 10 12 15
4 11 114 20
5 12 18 19
/*
标题:普通行列转换(version 2.0)
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-03-09
地点:广东深圳
说明:普通行列转换(version 1.0)仅针对sql server 2000提供静态和动态写法,version 2.0增加sql server 2005的有关写法。问题:假设有张学生成绩表(tb)如下:
姓名 课程 分数
张三 语文 74
张三 数学 83
张三 物理 93
李四 语文 74
李四 数学 84
李四 物理 94
想变成(得到如下结果):
姓名 语文 数学 物理
---- ---- ---- ----
李四 74 84 94
张三 74 83 93
-------------------
*/create table tb(姓名 varchar(10) , 课程 varchar(10) , 分数 int)
insert into tb values('张三' , '语文' , 74)
insert into tb values('张三' , '数学' , 83)
insert into tb values('张三' , '物理' , 93)
insert into tb values('李四' , '语文' , 74)
insert into tb values('李四' , '数学' , 84)
insert into tb values('李四' , '物理' , 94)
go--SQL SERVER 2000 静态SQL,指课程只有语文、数学、物理这三门课程。(以下同)
select 姓名 as 姓名 ,
max(case 课程 when '语文' then 分数 else 0 end) 语文,
max(case 课程 when '数学' then 分数 else 0 end) 数学,
max(case 课程 when '物理' then 分数 else 0 end) 物理
from tb
group by 姓名--SQL SERVER 2000 动态SQL,指课程不止语文、数学、物理这三门课程。(以下同)
declare @sql varchar(8000)
set @sql = 'select 姓名 '
select @sql = @sql + ' , max(case 课程 when ''' + 课程 + ''' then 分数 else 0 end) [' + 课程 + ']'
from (select distinct 课程 from tb) as a
set @sql = @sql + ' from tb group by 姓名'
exec(@sql) --SQL SERVER 2005 静态SQL。
select * from (select * from tb) a pivot (max(分数) for 课程 in (语文,数学,物理)) b--SQL SERVER 2005 动态SQL。
declare @sql varchar(8000)
select @sql = isnull(@sql + '],[' , '') + 课程 from tb group by 课程
set @sql = '[' + @sql + ']'
exec ('select * from (select * from tb) a pivot (max(分数) for 课程 in (' + @sql + ')) b')动态和静态的经典例子
select [index],wdy=max(case when xm='wdy' then hz else 0 end),
hzs=max(case when xm='hzs' then hz else 0 end),
xjb=max(case when xm='xjb' then hz else 0 end)
from table_1 group by [index] id index xm xm1 xm2 hz
----------- ----------- -------------------------------------------------- ----------- ----------- -----------
1 3 wdy 5 5 10
2 3 hzs 5 5 12
3 3 xjb 4 6 15
4 4 wdy 5 5 11
5 4 hzs 5 5 114
6 4 xjb 5 5 20
7 5 wdy 4 6 12
8 5 hzs 5 5 18
9 5 xjb 4 6 19 (9 行受影响) index wdy hzs xjb
----------- ----------- ----------- -----------
3 10 12 15
4 11 114 20
5 12 18 19
[index],
wdy=SUM(case when xm='wdy' then hz else 0 end),
hzs=SUM(case when xm='hzs' then hz else 0 end),
xjb=SUM(case when xm='xjb' then hz else 0 end)
from
tb
group by [index]
go
create table [tb]([id] int,[index] int,[xm] varchar(3),[xm1] numeric(2,1),[xm2] numeric(2,1),[hz] numeric(5,2))
insert [tb]
select 8,3,'wdy',5.0,5.0,10.00 union all
select 9,3,'hzs',5.0,5.0,12.00 union all
select 10,3,'xjb',4.0,6.0,15.00 union all
select 16,4,'wdy',5.0,5.0,11.00 union all
select 17,4,'hzs',5.0,5.0,114.00 union all
select 18,4,'xjb',5.0,5.0,20.00 union all
select 19,5,'wdy',4.0,6.0,12.00 union all
select 20,5,'hzs',5.0,5.0,18.00 union all
select 21,5,'xjb',4.0,6.0,19.00
select * from (select [index], xm ,hz from tb) m
pivot
(
sum(hz) for xm in(wdy,hzs,xjb)
) p/*
index wdy hzs xjb
----------- --------------------------------------- --------------------------------------- ---------------------------------------
3 10.00 12.00 15.00
4 11.00 114.00 20.00
5 12.00 18.00 19.00(3 行受影响)
*/
select @sqlstr=''
select @sqlstr=@sqlstr+',sum(case when xm='''+xm +''' then xm1+xm2 else 0 end) ['+xm +']'
from (select distinct xm from AA) a
select @sqlstr='select index'+@sqlstr +' from AA group by index'
exec(@sqlstr)结果集应该就是你想要的结果
drop table [tb]
go
create table [tb]([id] int,[index] int,[xm] varchar(3),[xm1] numeric(2,1),[xm2] numeric(2,1))
insert [tb]
select 8,3,'wdy',5.0,5.0 union all
select 9,3,'hzs',5.0,5.0 union all
select 10,3,'xjb',4.0,6.0 union all
select 16,4,'wdy',5.0,5.0 union all
select 17,4,'hzs',5.0,5.0 union all
select 18,4,'xjb',5.0,5.0 union all
select 19,5,'wdy',4.0,6.0 union all
select 20,5,'hzs',5.0,5.0 union all
select 21,5,'xjb',4.0,6.0
go
select
[index],
wdy=max(case when xm='wdy' then xm1+xm2 else 0 end),
hzs=max(case when xm='hzs' then xm1+xm2 else 0 end),
xjb=max(case when xm='xjb' then xm1+xm2 else 0 end)
from tb
group by [index]
/*(9 行受影响)
index wdy hzs xjb
----------- --------------------------------------- --------------------------------------- ---------------------------------------
3 10.0 10.0 10.0
4 10.0 10.0 10.0
5 10.0 10.0 10.0(3 行受影响)
*/
-- -----------t_mac 小编-------------------
--------------------希望有天成为大虾----
-- =========================================if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[index] int,[xm] varchar(3),[xm1] numeric(2,1),[xm2] numeric(2,1),[hz] numeric(5,2))
insert [tb]
select 8,3,'wdy',5.0,5.0,10.00 union all
select 9,3,'hzs',5.0,5.0,12.00 union all
select 10,3,'xjb',4.0,6.0,15.00 union all
select 16,4,'wdy',5.0,5.0,11.00 union all
select 17,4,'hzs',5.0,5.0,114.00 union all
select 18,4,'xjb',5.0,5.0,20.00 union all
select 19,5,'wdy',4.0,6.0,12.00 union all
select 20,5,'hzs',5.0,5.0,18.00 union all
select 21,5,'xjb',4.0,6.0,19.00
go
--2005
select *
from (select [index], xm ,hz from tb) m
pivot
(
max(hz) for xm in(wdy,hzs,xjb)
) K
--2000
select
[index],
wdy=SUM(case when xm='wdy' then hz else 0 end),
hzs=SUM(case when xm='hzs' then hz else 0 end),
xjb=SUM(case when xm='xjb' then hz else 0 end)
from
tb
group by [index]
go
...
go
create table [tb]([id] int,[index] int,[xm] varchar(3),[xm1] numeric(2,1),[xm2] numeric(2,1),[hz] numeric(5,2))
insert [tb]
select 8,3,'wdy',5.0,5.0,10.00 union all
select 9,3,'hzs',5.0,5.0,12.00 union all
select 10,3,'xjb',4.0,6.0,15.00 union all
select 16,4,'wdy',5.0,5.0,11.00 union all
select 17,4,'hzs',5.0,5.0,114.00 union all
select 18,4,'xjb',5.0,5.0,20.00 union all
select 19,5,'wdy',4.0,6.0,12.00 union all
select 20,5,'hzs',5.0,5.0,18.00 union all
select 21,5,'xjb',4.0,6.0,19.00select
[index],
wdy=SUM(case when xm='wdy' then hz else 0 end),
hzs=SUM(case when xm='hzs' then hz else 0 end),
xjb=SUM(case when xm='xjb' then hz else 0 end)
from
tb
group by [index]
(所影响的行数为 9 行)index wdy hzs xjb
----------- ---------------------------------------- ---------------------------------------- ----------------------------------------
3 10.00 12.00 15.00
4 11.00 114.00 20.00
5 12.00 18.00 19.00(所影响的行数为 3 行)
--原始数据如下
if object_id('tb') is not null drop table tb
create table tb(quyu nvarchar(20), yf nvarchar(7),
jhje decimal(14,2), xsje decimal(14,2), xscb decimal(14,2),
kcje decimal(14,2), ml decimal(14,2),mll decimal(14,2))
insert tb
select '安徽', '2009-01', 100.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '安徽', '2009-02', 200.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '安徽', '2009-03', 300.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '安徽2', '2009-04', 400.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '安徽2', '2009-05', 500.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '安徽2', '2009-06', 600.00, 0.00, 0.00, 0.00, 0.00, 0.00union all select '北京', '2009-01', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '北京', '2009-02', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '北京', '2009-03', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '北京2', '2009-04', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '北京2', '2009-05', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '北京2', '2009-06', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00union all select '云南', '2009-01', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '云南', '2009-02', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '云南', '2009-03', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '云南2', '2009-04', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '云南2', '2009-05', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '云南2', '2009-06', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00union all select '四川', '2009-01', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '四川', '2009-02', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '四川', '2009-03', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '四川2', '2009-04', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '四川2', '2009-05', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '四川2', '2009-06', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00union all select '上海', '2009-01', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '上海', '2009-02', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '上海', '2009-03', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '上海2', '2009-04', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '上海2', '2009-05', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '上海2', '2009-06', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00union all select '深圳', '2009-01', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '深圳', '2009-02', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '深圳', '2009-03', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '深圳2', '2009-04', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '深圳2', '2009-05', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '深圳2', '2009-06', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00union all select '成都', '2009-01', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '成都', '2009-02', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '成都', '2009-03', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '成都2', '2009-04', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '成都2', '2009-05', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '成都2', '2009-06', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00union all select '湛江', '2009-01', 12.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '湛江', '2009-02', 13.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '湛江', '2009-03', 14.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '湛江2', '2009-04', 15.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '湛江2', '2009-05', 16.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '湛江2', '2009-06', 17.00, 0.00, 0.00, 0.00, 0.00, 0.00union all select '广州', '2009-01', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '广州', '2009-02', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '广州', '2009-03', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '广州2', '2009-04', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '广州2', '2009-05', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
union all select '广州2', '2009-06', 0.00, 0.00, 0.00, 0.00, 0.00, 0.00/*
得到如下结果
月份 安微_jhje 安微_xsje 安微_xscb 安微_kcje 安微_ml 安微_mll 北京_jhje 北京_xsje 北京_xscb 北京_kcje 北京_ml 北京_mll ..........
2009-01 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00........
2009-02 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00........
2009-03 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00........
2009-04 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00........
2009-05 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00........
2009-06 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00........
................................................................................................................................................
总计 500 ..........................................................................................................................
*/create proc dbo.hljcbb -- exec dbo.hljcbb
as
declare @sql_head nvarchar(4000),
@sql_foot nvarchar(4000),
@sql_body nvarchar(4000),
@sql_variable_definition nvarchar(4000),
@sql_variable_init nvarchar(4000),
@sql_variable_set nvarchar(4000),
@groups varchar(100)-->>>>>>1、确定需要定义的变量个数>>>>>>>>>drop table #1
select
id=identity(int,0,1),
gs=0,
fieldvalue=CAST(
N','+quotename(quyu+'_jhje')
+N'=max(case when quyu='+quotename(quyu,N'''')
+N' then jhje else 0 end)'+CHAR(13)+
+N','+quotename(quyu+'_xsje')
+N'=max(case when quyu='+quotename(quyu,N'''')
+N' then xsje else 0 end)'+CHAR(13)+
+N','+quotename(quyu+'_xscb')
+N'=max(case when quyu='+quotename(quyu,N'''')
+N' then xscb else 0 end)'+CHAR(13)+
+N','+quotename(quyu+'_kcje')
+N'=max(case when quyu='+quotename(quyu,N'''')
+N' then kcje else 0 end)'+CHAR(13)+
+N','+quotename(quyu+'_ml')
+N'=max(case when quyu='+quotename(quyu,N'''')
+N' then ml else 0 end)'+CHAR(13)+
+N','+quotename(quyu+'_mll')
+N'=max(case when quyu='+quotename(quyu,N'''')
+N' then mll else 0 end)'
as Nvarchar(4000))
into #1 from tb
group by quyu
-->>>>>2、分临时时表,通过G字段的值决定当前的处理代码应保存在那个变量中
update A
set @groups=id/l,gs=@groups
from #1 a
cross join(select l=3800/max(LEN(FIELDVALUE)) from #1)b-->>>>>>3.生成数据处理语句--select * from #1
select @sql_head=N''''+REPLACE('select yf ',N'''',N'''''')+N'''',
@sql_foot=N''''+REPLACE(' from tb group by yf',N'''',N'''''')+N'''',
@sql_variable_definition=N'',
@sql_variable_init=N'',
@sql_variable_set=N'',
@sql_body=N''
while @groups>=0
select
@sql_variable_definition=N',@'+@groups+N' nvarchar(4000)'+@sql_variable_definition,
@sql_variable_init=N',@'+@groups+N'=N''''',
@sql_variable_set=N',@'+@groups+N'=case
when gs='+@groups+N' then @'+@groups+N'+fieldvalue
else @'+@groups+N'
end'+@sql_variable_set,
@sql_body=N'+@'+@groups+@sql_body,
@groups=@groups-1--去掉各变量中多余的前导符号
select
@sql_variable_definition=STUFF(@sql_variable_definition,1,1,N''), --stuff删除指定位置开始,指定数目的字符串,并在指定位置插入字符
@sql_variable_init=STUFF(@sql_variable_init,1,1,N''),
@sql_variable_set=STUFF(@sql_variable_set,1,1,N''),
@sql_body=STUFF(@sql_body,1,1,N'')
print @sql_head
print @sql_foot
print @sql_variable_definition
print @sql_variable_set
print @sql_variable_init
print @sql_body
--执行
exec(N'declare '+@sql_variable_definition+
N'select '+@sql_variable_init+
N'select '+@sql_variable_set+
N' from #1
exec(N'+@sql_head+N'
+'+@sql_body+N'
+'+@sql_foot+N')')
go
/*
yf 安徽_jhje 安徽_xsje 安徽_xscb 安徽_kcje 安徽_ml 安徽_mll 安徽2_jhje 安徽2_xsje 安徽2_xscb 安徽2_kcje 安徽2_ml 安徽2_mll
2009-01 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
2009-02 200.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
2009-03 300.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
2009-04 0.00 0.00 0.00 0.00 0.00 0.00 400.00 0.00 0.00 0.00 0.00 0.00
2009-05 0.00 0.00 0.00 0.00 0.00 0.00 500.00 0.00 0.00 0.00 0.00 0.00
2009-06 0.00 0.00 0.00 0.00 0.00 0.00 600.00 0.00 0.00 0.00 0.00 0.00
*/
--原始数据如下
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[index] int,[xm] varchar(3),[xm1] numeric(2,1),[xm2] numeric(2,1),[hz] numeric(5,2))
insert [tb]
select 8,3,'wdy',5.0,5.0,10.00 union all
select 9,3,'hzs',5.0,5.0,12.00 union all
select 10,3,'xjb',4.0,6.0,15.00 union all
select 16,4,'wdy',5.0,5.0,11.00 union all
select 17,4,'hzs',5.0,5.0,114.00 union all
select 18,4,'xjb',5.0,5.0,20.00 union all
select 19,5,'wdy',4.0,6.0,12.00 union all
select 20,5,'hzs',5.0,5.0,18.00 union all
select 21,5,'xjb',4.0,6.0,19.00
--------------开始查询--------------------------
declare @sql varchar(8000)
set @sql =
select @sql = @sql + ' , sum(case xm when ''' + xm + ''' then hz else 0 end) [' + xm + ']'set @sql = @sql +
exec(@sql)
alter proc dbo.hljcbb -- exec dbo.hljcbb
as
declare @sql_head nvarchar(4000),
@sql_foot nvarchar(4000),
@sql_body nvarchar(4000),
@sql_variable_definition nvarchar(4000),
@sql_variable_init nvarchar(4000),
@sql_variable_set nvarchar(4000),
@groups varchar(100)-->>>>>>1、确定需要定义的变量个数>>>>>>>>>drop table #1
select
id=identity(int,0,1),
gs=0,
fieldvalue=CAST(
N','+quotename(xm)
+N'=max(case when xm='+quotename(xm,N'''')
+N' then hz else 0 end)'
as Nvarchar(4000))
into #1 from (select distinct xm from tb) as a --要修改的临时表生成部分
group by [xm]
--select * from #1
-->>>>>2、分临时时表,通过G字段的值决定当前的处理代码应保存在那个变量中
update A
set @groups=id/l,gs=@groups
from #1 a
cross join(select l=3800/max(LEN(FIELDVALUE)) from #1)b-->>>>>>3.生成数据处理语句--select * from #1
select @sql_head=N''''+REPLACE('select [index]',N'''',N'''''')+N'''', --要修改的部分
@sql_foot=N''''+REPLACE(' from tb group by [index]',N'''',N'''''')+N'''', --要修改的部分
@sql_variable_definition=N'',
@sql_variable_init=N'',
@sql_variable_set=N'',
@sql_body=N''
while @groups>=0
select
@sql_variable_definition=N',@'+@groups+N' nvarchar(4000)'+@sql_variable_definition,
@sql_variable_init=N',@'+@groups+N'=N''''',
@sql_variable_set=N',@'+@groups+N'=case
when gs='+@groups+N' then @'+@groups+N'+fieldvalue
else @'+@groups+N'
end'+@sql_variable_set,
@sql_body=N'+@'+@groups+@sql_body,
@groups=@groups-1--去掉各变量中多余的前导符号
select
@sql_variable_definition=STUFF(@sql_variable_definition,1,1,N''), --stuff删除指定位置开始,指定数目的字符串,并在指定位置插入字符
@sql_variable_init=STUFF(@sql_variable_init,1,1,N''),
@sql_variable_set=STUFF(@sql_variable_set,1,1,N''),
@sql_body=STUFF(@sql_body,1,1,N'')
print @sql_head
print @sql_foot
print @sql_variable_definition
print @sql_variable_set
print @sql_variable_init
print @sql_body
--执行
exec(N'declare '+@sql_variable_definition+
N'select '+@sql_variable_init+
N'select '+@sql_variable_set+
N' from #1
exec(N'+@sql_head+N'
+'+@sql_body+N'
+'+@sql_foot+N')')
goexec dbo.hljcbb --执行存储过程
/*
结果
index hzs wdy xjb
3 12.00 10.00 15.00
4 114.00 11.00 20.00
5 18.00 12.00 19.00
*/
declare @table table (id int,[index] int,xm varchar(3),xm1 numeric(2,1),xm2 numeric(2,1),hz numeric(5,2))
insert into @table
select 8,3,'wdy',5.0,5.0,10.00 union all
select 9,3,'hzs',5.0,5.0,12.00 union all
select 10,3,'xjb',4.0,6.0,15.00 union all
select 16,4,'wdy',5.0,5.0,11.00 union all
select 17,4,'hzs',5.0,5.0,114.00 union all
select 18,4,'xjb',5.0,5.0,20.00 union all
select 19,5,'wdy',4.0,6.0,12.00 union all
select 20,5,'hzs',5.0,5.0,18.00 union all
select 21,5,'xjb',4.0,6.0,19.00select
[index],
wdy=max(case when xm='wdy' then hz else 0 end),
hzs=max(case when xm='hzs' then hz else 0 end),
xjb=max(case when xm='xjb' then hz else 0 end)
from @table
group by [index] /*
index wdy hzs xjb
----------- --------------------------------------- --------------------------------------- -------
3 10.00 12.00 15.00
4 11.00 114.00 20.00
5 12.00 18.00 19.00
*/