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

Taking a vertical list and transposing it to horizontal and then grouping Expand / Collapse
Author
Message
Posted Friday, December 28, 2012 7:19 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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

Is this possible?

I have several columns, such as:

Coulmn A

red
green
yellow
black

Column B

shirt
jumper
blouse
coat


Column C

for women
for men
for girls
extra large



I have permutated the columns in such a way that it gives me the following result:

red shirt for women
red shirt for men
red shirt for girls
red shirt extra large
....
black coat for girls
black coat extra large


using a cross join.



Now what I would like to do, if possible, is take all the results where it reads,

red shirt for women, red shirt for men, red shirt for girls, red shirt exta large
green shirt for women, green shirt for men, green shirt for girls, green shirt exta large
...
black coat for women, black coat for men, black coat for girls, black coat extra large

basically where it groups then by column a, which is the main word(s). by transposing the cross joined words and put them on the same row, contactenated with commas.

hopefully this makes sense?

Thanks
Post #1401106
Posted Friday, December 28, 2012 9:37 PM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 4:51 PM
Points: 32,923, Visits: 26,811
I suspect the reason why you didn't get much help on your last similar post is because people don't have the time to setup the test data. Please see the article at the first link in my signature line below for how to post data in a readily consumable format. Once you've done that, post your Cross Join code with the readily consumable data and I'm sure that someone will jump up to help you.

--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 #1401116
Posted Friday, December 28, 2012 9:59 PM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Saturday, May 18, 2013 4:20 PM
Points: 479, Visits: 406
Here is the code


if OBJECT_ID('tempdb..#test') is not null drop table #test
go

create table #test
( column1 varchar(100),
column2 varchar(100),
column3 varchar(100)
)
go

insert into #test
select 'red' as Column1, 'shirt' as Column2, 'for women' as Column3 union all
select 'green', 'jumper', 'for men' union all
select 'yellow', 'blouse', 'for girls' union all
select 'black', 'coat', 'extra large'
go

declare @Result varchar(4000) = ''

select @Result = @Result + A.column1 + space(1) + B.column2 + space(1) + C.column3 + ', '
from #test A
cross join #test B
cross join #test C


select left(@Result,LEN(ltrim(rtrim(@Result)))-1) as FinalResult

if OBJECT_ID('tempdb..#test') is not null drop table #test
go


Post #1401120
Posted Monday, December 31, 2012 3:29 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, May 20, 2013 12:37 AM
Points: 1,049, Visits: 1,439
Satish's Query is partially correct but it doesn't break the records on th basis of Column1. You would nee to do a Correlated Subquery for that as follows:

--Creating Table 

create table #test
( column1 varchar(100),
column2 varchar(100),
column3 varchar(100)
)


--Inserting Sample Data

insert into #test
select 'red' as Column1, 'shirt' as Column2, 'for women' as Column3 union all
select 'green', 'jumper', 'for men' union all
select 'yellow', 'blouse', 'for girls' union all
select 'black', 'coat', 'extra large'


--Query

Select STUFF
(
(select DISTINCT ',' + A.column1 + ' ' + B.column2 + ' ' + C.column3 from #test As A cross join #test As B
cross join #test As C Where A.column1 = p.column1 For XML Path('')),1,1,''
)
From #test As p



Hope this is what you are looking for.


Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden
Post #1401343
Posted Tuesday, January 01, 2013 7:37 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 6:03 AM
Points: 2,345, Visits: 3,191
vinu512 (12/31/2012)
Satish's Query is partially correct but it doesn't break the records on th basis of Column1. You would nee to do a Correlated Subquery for that as follows:

--Creating Table 

create table #test
( column1 varchar(100),
column2 varchar(100),
column3 varchar(100)
)


--Inserting Sample Data

insert into #test
select 'red' as Column1, 'shirt' as Column2, 'for women' as Column3 union all
select 'green', 'jumper', 'for men' union all
select 'yellow', 'blouse', 'for girls' union all
select 'black', 'coat', 'extra large'


--Query

Select STUFF
(
(select DISTINCT ',' + A.column1 + ' ' + B.column2 + ' ' + C.column3 from #test As A cross join #test As B
cross join #test As C Where A.column1 = p.column1 For XML Path('')),1,1,''
)
From #test As p



Hope this is what you are looking for.


Hey Vinu! Happy New Year!

I think if you don't do 2 CROSS JOINs you can eliminate the need for DISTINCT as follows:

SELECT STUFF((
SELECT ',' + a.Column1 + ' ' + MyString
FROM (
SELECT MyString= a.Column2 + ' ' + b.Column3
FROM #test a CROSS JOIN #test b) b
FOR XML PATH('')), 1, 1, '')
FROM #Test a


Uses your test data setup.



No loops! No CURSORs! No RBAR! Hoo-uh!

INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?

Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Are you too recursively challenged?
Splitting strings based on patterns can be fast!
Post #1401639
Posted Tuesday, January 01, 2013 10:17 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, May 20, 2013 12:37 AM
Points: 1,049, Visits: 1,439
dwain.c (1/1/2013)
vinu512 (12/31/2012)
Satish's Query is partially correct but it doesn't break the records on th basis of Column1. You would nee to do a Correlated Subquery for that as follows:

--Creating Table 

create table #test
( column1 varchar(100),
column2 varchar(100),
column3 varchar(100)
)


--Inserting Sample Data

insert into #test
select 'red' as Column1, 'shirt' as Column2, 'for women' as Column3 union all
select 'green', 'jumper', 'for men' union all
select 'yellow', 'blouse', 'for girls' union all
select 'black', 'coat', 'extra large'


--Query

Select STUFF
(
(select DISTINCT ',' + A.column1 + ' ' + B.column2 + ' ' + C.column3 from #test As A cross join #test As B
cross join #test As C Where A.column1 = p.column1 For XML Path('')),1,1,''
)
From #test As p



Hope this is what you are looking for.


Hey Vinu! Happy New Year!

I think if you don't do 2 CROSS JOINs you can eliminate the need for DISTINCT as follows:

SELECT STUFF((
SELECT ',' + a.Column1 + ' ' + MyString
FROM (
SELECT MyString= a.Column2 + ' ' + b.Column3
FROM #test a CROSS JOIN #test b) b
FOR XML PATH('')), 1, 1, '')
FROM #Test a


Uses your test data setup.


Hi Dwain,

Happy New Year!!!
Yes you are right Dwain.....I didn't go that deep into the solution. I just transformed the solution given by SatishAiyyar using STUFF.


Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden
Post #1401651
Posted Saturday, January 05, 2013 1:22 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, April 21, 2013 9:55 PM
Points: 17, Visits: 60
Thanks Jeff for englightening me on the benefit to the forum members of using create table / select/insert statements for readily consumable data. This is all new to me in fact using those statement, but I think from the examples from the other forum members I understand this now, so hopefully going forward, I will use them more effectively when posting to the forum.

Thanks vinu512 for posting the initial solution, along with showing me the correct way to use the create table function.

Thanks Dwain for posting you're solution, as I believe this is what I was looking for.

Few Questions, Is Stuff a valid tsql function? As I didn't see that in the insert / create table function that Vinu created. So not clear how that is working?

Also when creating this tables, I noticed these tables where being generated in the system db , tempdb, under master., under temporary tables.


I assume its safe to delete these temporary tables under master?

Lastly, going forward, when requesting help when creating tables, any way to assign the this to a temporary db i create of my own so it doesn't put into master. maybe something like

use mytempdb
create table #test



Thanks ALL. You're help is most appreciated
Post #1403277
Posted Sunday, January 06, 2013 9:32 AM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 4:51 PM
Points: 32,923, Visits: 26,811
VegasL (1/5/2013)
Thanks Jeff for englightening me on the benefit to the forum members of using create table / select/insert statements for readily consumable data. This is all new to me in fact using those statement, but I think from the examples from the other forum members I understand this now, so hopefully going forward, I will use them more effectively when posting to the forum.

Thanks vinu512 for posting the initial solution, along with showing me the correct way to use the create table function.

Thanks Dwain for posting you're solution, as I believe this is what I was looking for.

Few Questions, Is Stuff a valid tsql function? As I didn't see that in the insert / create table function that Vinu created. So not clear how that is working?

Also when creating this tables, I noticed these tables where being generated in the system db , tempdb, under master., under temporary tables.


I assume its safe to delete these temporary tables under master?

Lastly, going forward, when requesting help when creating tables, any way to assign the this to a temporary db i create of my own so it doesn't put into master. maybe something like

use mytempdb
create table #test



Thanks ALL. You're help is most appreciated


Hmmm... it sounds like you're not just really new this forum but maybe really new to the world of SQL itself. Here's a hint that really helped me when I was first starting out.

One of your best friends in the world should be a thing called "Books OnLine" (abbreviated as simply BOL). You can easily get there from SSMS just by pressing the {f1} key. Then you can lookup things like what "STUFF" actually is (yes, it's a real function) and more information on temporary tables and where they're actually created at no matter what the current database is. Even with over 16 years of experience, I still use BOL several times a day if, for nothing else, just to make sure of things.


--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 #1403335
Posted Monday, January 07, 2013 2:08 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, May 20, 2013 12:37 AM
Points: 1,049, Visits: 1,439
VegasL (1/5/2013)
Thanks vinu512 for posting the initial solution, along with showing me the correct way to use the create table function.


I'm glad I was of some help.
You should also thank SSC for making it possible for you to get advice from people like Jeff and a lot more other people who devote a lot of their time to this forum.


Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden
Post #1403484
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse