如何以3 * 3矩阵形式显示行 [英] how to show rows in 3*3 matrix form
本文介绍了如何以3 * 3矩阵形式显示行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨..
我有一张数据表
id
-
1
2
3
4
5
$
7
8
9
但我需要结果
col1 | col2 | col3
---------------
1 | 2 | 3
--------------
4 | 5 | 6
--------------
7 | 8 | 9
解决方案
请试试这个.....这不是更有效的代码,但满足你的需求
声明 @ demo table (id int identity ( 1 , 1 ),columname int null )
插入 进入 @ demo (columname)
选择 yourcolumn 来自 yourtable
声明 @ temp table (kid < span class =code-keyword> int identity (1 , 1 ),col1 int null ,col2 int null ,col3 int null )
声明 @ i int = 1
声明 @ count int = 0
声明 @ j int = 1
声明 @ m int = 0
set @count =( select count(*) from h_ptmast)
while (@ i< = @ count) begin
如果跨度>(@ j = 1)开始
插入 进入 @ temp (col1)
选择 columname 来自 @ demo 其中 id = @ i
set @ m = @ m + 1
end
else if (@ j = 2) begin
update @ temp set col2 = columname
< span class =code-keyword> from @ demo 其中 id = @ i < span class =code-keyword>和 kid = @ m
end
其他 开始
更新 @ temp set col3 = columname
来自 @ demo 其中 id = @ i 和 kid = @ m
结束
如果 (@ j = 3)开始
set @ j = 1
end
else 开始
set @ j = @ j + 1
end
set @ i = @ i + 1
end
选择 col1,col2,col3 来自 @ temp
Hi..
I have a table with data
id
--
1
2
3
4
5
6
7
8
9
but i need result as
col1| col2|col3
---------------
1 | 2 | 3
--------------
4 | 5 | 6
--------------
7 | 8 | 9
解决方案
plz try this .....This is not more efficent code but fullfill your need
declare @demo table(id int identity(1,1),columname int null) insert into @demo(columname) select yourcolumn from yourtable declare @temp table(kid int identity(1,1),col1 int null,col2 int null,col3 int null) declare @i int=1 declare @count int=0 declare @j int=1 declare @m int=0 set @count=(select count(*) from h_ptmast) while(@i<=@count) begin if(@j=1)begin insert into @temp(col1) select columname from @demo where id=@i set @m=@m+1 end else if(@j=2)begin update @temp set col2=columname from @demo where id=@i and kid=@m end else begin update @temp set col3=columname from @demo where id=@i and kid=@m end if(@j=3)begin set @j=1 end else begin set @j=@j+1 end set @i=@i+1 end select col1,col2,col3 from @temp
这篇关于如何以3 * 3矩阵形式显示行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文