Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Permutations of Columns in a Table Expand / Collapse
Author
Message
Posted Sunday, December 23, 2012 11:25 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, April 21, 2013 9:55 PM
Points: 17, Visits: 60
Hello,

I have several columns, such as:

Coulmn A

red
green
yellow
black
blue green
pink

Column B

shirt
jumper
blouse
coat
t-shirt
skirt

Column C

for women
for men
for girls
extra large
for boys
one size fits all



I need to permutate, it such a way that gives me the following result:

red shirt for women
red shirt for men
red shirt for girls
red shirt extra large
red shirt for boys
red shirt one size fits all
red jumper for women
red jumper for men
...
pink skirt for boys
pink skirt one size fits all


I was able to put each column in 3 separate tables, and then do a simple cross join but would like to have all the columns in one table and then the result.

Thanks
Post #1399761
Posted Sunday, December 23, 2012 5:36 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:01 PM
Points: 33,111, Visits: 27,037
VegasL (12/23/2012)
pink skirt for boys


There's just something so wrong with that!

The answer is to do a Cross Join between all the values of all 3 columns with 3 aliased copies of the same table. If you'd like a coded answer, please read and heed the article at the first link in my signature line below. Thanks.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1399775
Posted Sunday, December 23, 2012 6:14 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, April 21, 2013 9:55 PM
Points: 17, Visits: 60
Hi John,

Yes I agree something wrong with that. I'm working with large data set, didn't even notice.

Anyways, I did read the link you posted on forum etiqutte so hopefully I am complying now.

I put the data in .xls format for ease of use. Hopefully thats what you're were asking for.

Yes, coded answer would be appreciated.

Thanks


  Post Attachments 
Permutation KW's Cross Join.xlsx (11 views, 9.58 KB)
Post #1399782
Posted Sunday, December 23, 2012 10:28 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:01 PM
Points: 33,111, Visits: 27,037
VegasL (12/23/2012)
Hi John,

Yes I agree something wrong with that. I'm working with large data set, didn't even notice.

Anyways, I did read the link you posted on forum etiqutte so hopefully I am complying now.

I put the data in .xls format for ease of use. Hopefully thats what you're were asking for.

Yes, coded answer would be appreciated.

Thanks


First of all, the name is "Jeff".

Second, please read the article again. Having this stuff in a spreadsheet doesn't make it any easier than what you've already posted. We need a CREATE TABLE and some INSERT/SELECT statements to make "readily consumable data".


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1399798
Posted Monday, December 24, 2012 6:17 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, April 21, 2013 9:55 PM
Points: 17, Visits: 60
Ok here's my attempt...

select [Colors] + ' ' + [Items] +' ' + [Size] as 'Combined KW'
from [dbo].[MYDB] as KW
cross join [Colors] cross join [Items] cross join [Size]

i'm trying to combine all 3 columns as 'combined kw'

Thanks


Post #1400009
Posted Friday, December 28, 2012 4:56 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, April 21, 2013 9:55 PM
Points: 17, Visits: 60
After spending some time dabbling with this, I finally figured it out :)

it required adding several self joins to the from line such as

select color.[colors] + ' ' + itm.[items] + ' ' +sz.[size] as 'Combined KW'
from [dbo].[MYDB] as [Color] cross join [Items] as ITM cross join [Size] as SZ



Post #1401094
Posted Monday, December 31, 2012 10:17 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, January 15, 2013 11:11 AM
Points: 1,945, Visits: 2,782
I'm trying to combine all 3 columns as 'combined kw'


Why? These are three different attributes; they get a separate column in a valid data model.

I would put the limited list of values of each one in a CHECK() constraint -- color_name VARCHAR(12) NOT NULL CHECK (color_name IN ('red', green', 'yellow', 'black', 'blue green', 'pink')).


Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1401432
Posted Friday, January 04, 2013 1:00 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, February 03, 2013 8:30 AM
Points: 2, Visits: 43
create table #temp23(color nvarchar(566),item nvarchar(455),size nvarchar(332))
insert into #temp23
select 'red','shirt','for women' union all
select 'green','jumper','for men' union all
select 'yellow','blouse','for girls' union all
select 'black','coat','extra large' union all
select 'blue green','t-shirt','for boys' union all
select 'pink','skirt','one size fits all'
select * from #temp23


create table #t(a nvarchar(566))
insert into #t select color from #temp23
create table #t1(b nvarchar(566))
insert into #t1 select item from #temp23
create table #t2(c nvarchar(566))
insert into #t2 select size from #temp23
select *,(a+' '+ b+' '+c) as total from #t cross join #t1 cross join #t2


---------------I am a begginer tried soft way--------------------------------

VegasL (12/23/2012)
Hello,

I have several columns, such as:

Coulmn A

red
green
yellow
black
blue green
pink

Column B

shirt
jumper
blouse
coat
t-shirt
skirt

Column C

for women
for men
for girls
extra large
for boys
one size fits all



I need to permutate, it such a way that gives me the following result:

red shirt for women
red shirt for men
red shirt for girls
red shirt extra large
red shirt for boys
red shirt one size fits all
red jumper for women
red jumper for men
...
pink skirt for boys
pink skirt one size fits all


I was able to put each column in 3 separate tables, and then do a simple cross join but would like to have all the columns in one table and then the result.

Thanks
Post #1403081
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse