| Author |
Topic |
|
sorcir24
Starting Member
7 Posts |
Posted - 2005-02-16 : 12:44:22
|
| Hello All!I need a little help to solve a Prob here!I have the table products with name"carpet""socks""t-shits""bed""shoes"and the select clause have to return thisname1 name2 name3 Carpet socks t-shirtsbed shoesThanks! |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-02-16 : 14:09:19
|
you basicaly want to split the data into 3 columns, right?that would probably be simpler in the front end.how much data do you have?Go with the flow & have fun! Else fight the flow |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-02-16 : 14:15:25
|
but if you must do it on the server this will do it:declare @MainTable table (id int identity(1,1), name varchar(20))insert into @MainTable (name)select 'carpet' union allselect 'socks' union allselect 't-shirts' union allselect 'bed' union allselect 'shoes'select *from @MainTableselect identity(int, 1,1) as id, nameinto #temp1from @MainTablewhere id%3=1select identity(int, 1,1) as id, nameinto #temp2from @MainTablewhere id%3=2select identity(int, 1,1) as id, nameinto #temp3from @MainTablewhere id%3=0select t1.name as name1, isnull(t3.name, '') as name2, isnull(t3.name, '') as name3from #temp1 t1 left join #temp2 t2 on t1.id = t2.id left join #temp3 t3 on t1.id = t3.iddrop table #temp1drop table #temp2drop table #temp3 Go with the flow & have fun! Else fight the flow |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-02-16 : 14:34:14
|
[code]declare @MainTable table (id int identity(1,1), name varchar(20))insert into @MainTable (name)select 'carpet' union allselect 'socks' union allselect 't-shirts' union allselect 'bed' union allselect 'shoes'select *from @MainTableSelect *From (Select distinct id = (Id-1)/3 From @mainTable) ALeft Join @mainTable BOn (A.id*3)+0 = B.id-1Left Join @mainTable COn (A.id*3)+1 = C.id-1Left Join @mainTable DOn (A.id*3)+2 = D.id-1[/code]Corey "If the only tool you have is a hammer, the whole world looks like a nail." - Mark Twain |
 |
|
|
sorcir24
Starting Member
7 Posts |
Posted - 2005-02-16 : 16:46:13
|
| Thank you all!but i´m sorry, the statement of declare @maintable table(etc) does not work in SQL Server 7! Spirit, do you know another method?Thanks! |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-02-16 : 17:03:15
|
This should work for Sql Sevrer 7... though I can't test it  Create Table MainTable (id int identity(1,1), name varchar(20))insert into MainTable (name)select 'carpet' union allselect 'socks' union allselect 't-shirts' union allselect 'bed' union allselect 'shoes'select *from MainTableSelect *From (Select distinct id = (Id-1)/3 From mainTable) ALeft Join mainTable BOn (A.id*3)+0 = B.id-1Left Join mainTable COn (A.id*3)+1 = C.id-1Left Join mainTable DOn (A.id*3)+2 = D.id-1Drop Table mainTable Corey "If the only tool you have is a hammer, the whole world looks like a nail." - Mark Twain |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-02-16 : 19:10:23
|
@maintable is irrelevant to your situation. you should replace @maintable with you table. me and corey used it just for examples.Go with the flow & have fun! Else fight the flow |
 |
|
|
sorcir24
Starting Member
7 Posts |
Posted - 2005-02-17 : 13:06:42
|
| Spirit and Seventhnightthank you for you help! :) ;) |
 |
|
|
sorcir24
Starting Member
7 Posts |
Posted - 2005-02-17 : 13:52:27
|
| Ok i have anothe one!i have the table detailsPN SN AA 011 AA 021 BB 041 BB 010 BB 011 BB 014 BB 016 BB 024 CC 014 CC 021 CC 048And i want to get PN SN1 SN2 SN3 SN4 AA 011 021 BB 041 010 011 014 BB 016 024 CC 014 021 048I was really trying hard with the method that you post here but i don´t get it!Please!!Thanks! |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-02-17 : 14:10:34
|
[code]Create Table MainTable (id int identity(1,1), PN varchar(10), SN varchar(10), seqn int)insert into MainTable (PN,SN)select 'AA', '011' union allselect 'AA', '021' union allselect 'BB', '041' union allselect 'BB', '010' union allselect 'BB', '014' union allselect 'BB', '016' union allselect 'BB', '024' union allselect 'CC', '014' union allselect 'CC', '021' union allselect 'CC', '048' Update MainTable set seqn = (Select count(*) From MainTable A Where PN = MainTable.PN and id < MainTable.id) from MainTableselect *from MainTableSelect A.PN, SN1 = B.SN, SN2 = C.SN, SN3 = D.SN, SN4 = E.SNFrom (select distinct seqn = seqn/4, PN from MainTable) ALeft Join mainTable BOn A.PN = B.PNand (A.seqn*4)+0 = B.seqnLeft Join mainTable COn A.PN = C.PNand (A.seqn*4)+1 = C.seqnLeft Join mainTable DOn A.PN = D.PNand (A.seqn*4)+2 = D.seqnLeft Join mainTable EOn A.PN = E.PNand (A.seqn*4)+3 = E.seqnOrder By A.PN, B.SN, C.SN, D.SNDrop Table mainTable[/code]Corey "If the only tool you have is a hammer, the whole world looks like a nail." - Mark Twain |
 |
|
|
sorcir24
Starting Member
7 Posts |
Posted - 2005-02-17 : 15:50:00
|
| Man... you are a genius! Thank you! |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-02-17 : 16:26:32
|
Not sure about that... but you're welcome Corey "If the only tool you have is a hammer, the whole world looks like a nail." - Mark Twain |
 |
|
|
|