If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Seperating a record into multiple

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-27-03, 09:00
riddelrp riddelrp is offline
Registered User
 
Join Date: Feb 2003
Location: Dayton, OH
Posts: 8
Seperating a record into multiple

Okay, I have a table (called "Sizes") that looks like so:

Size____Type1____Type2____Type3
27"______1________0________1
85"______0________1________0

How do I pull

Size_____Type
27"______Type1
27"______Type3
85"______Type2

Select ???? from Sizes ????

Thanks,
riddelrp
Reply With Quote
  #2 (permalink)  
Old 02-27-03, 09:55
riddelrp riddelrp is offline
Registered User
 
Join Date: Feb 2003
Location: Dayton, OH
Posts: 8
Reverse Pivot Table

Sounds like what I need is a reverse pivot table... now how do I do that?!

Quote:
Originally posted by riddelrp
Okay, I have a table (called "Sizes") that looks like so:

Size____Type1____Type2____Type3
27"______1________0________1
85"______0________1________0

How do I pull

Size_____Type
27"______Type1
27"______Type3
85"______Type2

Select ???? from Sizes ????

Thanks,
riddelrp
Reply With Quote
  #3 (permalink)  
Old 02-27-03, 10:04
Paul Young Paul Young is offline
Registered User
 
Join Date: Feb 2002
Location: Houston, TX
Posts: 809
something like this?

Code:
create table #Sizes(Size varchar(5), Type1 bit, Type2 bit, Type3 bit)
insert into #Sizes values('27"',1,0,1)
insert into #Sizes values('28"',0,1,0)
select * From #Sizes
select Size, 'Type1' as Type from #Sizes where Type1 > 0
union
select Size, 'Type2' as Type from #Sizes where Type2 > 0
union
select Size, 'Type3' as Type from #Sizes where Type3 > 0
 order by 1,2
__________________
Paul Young
(Knowledge is power! Get some!)
Reply With Quote
  #4 (permalink)  
Old 02-27-03, 10:11
msieben msieben is offline
Registered User
 
Join Date: Feb 2003
Location: Germany
Posts: 53
Cool Re: Seperating a record into multiple

the following will work:

select size, 'Type1' from sizes where type1=1
union
select size, 'Type2' from sizes where type2=1
union
select size, 'Type3' from sizes where type3=1
order by 1
Reply With Quote
  #5 (permalink)  
Old 02-27-03, 10:49
riddelrp riddelrp is offline
Registered User
 
Join Date: Feb 2003
Location: Dayton, OH
Posts: 8
Very cool, thanks... 1 last question.

what does order by 1,2 do?

Thanks,
Ryan

PS: I was starting to write a stored proc where I could dump all the Type1s in then all the Type2s then all the Type3s into a temp table then pull all records in that table (the union thing is MUCH easier)

thanks
Reply With Quote
  #6 (permalink)  
Old 02-27-03, 10:51
Paul Young Paul Young is offline
Registered User
 
Join Date: Feb 2002
Location: Houston, TX
Posts: 809
order by 1 or order by 1,2 = order by result set column 1, column 2 etc.
__________________
Paul Young
(Knowledge is power! Get some!)
Reply With Quote
  #7 (permalink)  
Old 02-27-03, 10:52
msieben msieben is offline
Registered User
 
Join Date: Feb 2003
Location: Germany
Posts: 53
Quote:
Originally posted by riddelrp
Very cool, thanks... 1 last question.

what does order by 1,2 do?

Thanks,
Ryan

PS: I was starting to write a stored proc where I could dump all the Type1s in then all the Type2s then all the Type3s into a temp table then pull all records in that table (the union thing is MUCH easier)

thanks
order by just oders the resultset
order by 1,2 means: order by "column1" , "column2"
Reply With Quote
  #8 (permalink)  
Old 02-27-03, 10:57
riddelrp riddelrp is offline
Registered User
 
Join Date: Feb 2003
Location: Dayton, OH
Posts: 8
oh wow, ok I get it, it orders by the column number... I'm sorry, I've just never done an order by statement using col numbers before

riddelrp making a name for himself on dbforums after just 4 posts... even if it isn't a good name :-/

-riddelrp
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On