SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Distribute each 3 rows in one column
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

cmosii
Starting Member

United Arab Emirates
1 Posts

Posted - 07/05/2012 :  05:26:05  Show Profile  Reply with Quote
Hi ...
I have a table which contains
ROW1 ROW2
ID File
============
1 1.file
2 2.file
3 3.file
4 4.file
5 5.file
6 6.file
7 7.file
8 8.file
============

is it possible to show it in the following format
Row1 Row2 Row3
1.file 2.file 3.file
4.file 5.file 6.file
7.file 8.file


...etc

Thank you for your cooperation

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 07/05/2012 :  05:47:52  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
something like

select a1.file, a2.file, a3.file
(select *, seq = id/3 from tbl where id%3 = 1) a1
left join (select *, seq = id/3 from tbl where id%3 = 2) a2
on a2.seq = a1.seq
left join (select *, seq = id/3 from tbl where id%3 = 0) a3
on a3.seq = a1.seq





==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48012 Posts

Posted - 07/05/2012 :  10:06:20  Show Profile  Reply with Quote

SELECT MAX(CASE WHEN Rn=1 THEN File END) AS Row1,
MAX(CASE WHEN Rn=2 THEN File END) AS Row2,
MAX(CASE WHEN Rn=3 THEN File END) AS Row3
FROM 
(SELECT ROW_NUMBER() OVER (PARTITION BY (ID-1)/3 ORDER BY ID) AS Rn,(ID-1)/3 AS Grp,*
FROM Table
)t
GROUP BY Grp 


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/


Edited by - visakh16 on 07/05/2012 10:06:58
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000