Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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
Posted
Updated 1-May-14 20:17pm
v2
Comments
_Asif_ 2-May-14 1:41am    
What have you done so far?

1 solution

plz try this .....This is not more efficent code but fullfill your need

SQL
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900