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 ««12

Get concatenated value for a column without cursor Expand / Collapse
Author
Message
Posted Wednesday, July 7, 2010 11:32 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Wednesday, August 13, 2014 9:41 AM
Points: 3,241, Visits: 4,999
Why to go hard way?
No need to Add column ColRowNo and update it.

I would go like this;

---------------------------------------------------------------------
-- Create Table and Insert data
---------------------------------------------------------------------
Create Table TestTable (RowID Int Identity(1, 1), Col1 int, Col2 char(3))

Insert TestTable Values (1, 'ABC')
Insert TestTable Values (1, 'DEF')
Insert TestTable Values (1, 'GHI')
Insert TestTable Values (2, 'JKL')
Insert TestTable Values (2, 'MNO')
Insert TestTable Values (3, 'PQR')
Insert TestTable Values (3, 'STU')

select * from TestTable
---------------------------------------------------------------------
-- Add TestTable column and Insert Row Number
---------------------------------------------------------------------

Select Distinct Col1, (Stuff((Select ',' + Col2 from TestTable where Col1 = a.Col1 for XML Path('')),1,1,''))
from TestTable a

Drop Table TestTable
GO






----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sometimes, winning is not an issue but trying.

You can check my BLOG here

Post #949006
Posted Thursday, July 8, 2010 10:35 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, August 18, 2014 11:49 AM
Points: 273, Visits: 442
Alright, so with all the different ways of doing this, I decided to run some tests and see which way would be most beneficial.

I set out to try 1 million (raise pinky finger to lips) rows of data. However, my data generator failed after 127k ish rows. So I used them to test all three of the presented ways of doing this. I had 10,000 groups in 127,971 rows of data. When I ran the statements a couple of things jumped out at me. First, it seemed that the execution plans for Nitin's Alter / Update / Select were coming in drastically under the other plans. I made a small change to Nigel's plan, and ran it against Nitin's.

Nitin's plan came in at 20% of the batch (12% for the Alter / Update, 8% for Select), while Nigel's plan came in with the remaining 80%. I also noticed that Nitin's plan, for 127k records, was returning with a time right around 4.5 seconds, while the modified Nigel's plan was coming in just over half a second. (Atif's plan was coming in close to Nitin's, so I stopped looking there.)

While I'm no expert on performance, I usually check things like this out if I'm not sure which solution to choose. My question is this: What matters more,in large sets of data, execution time, or execution cost?
Post #949441
Posted Thursday, July 8, 2010 10:48 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Wednesday, August 13, 2014 9:41 AM
Points: 3,241, Visits: 4,999
My question is this: What matters more,in large sets of data, execution time, or execution cost?


As a DB guy, I think both of them are important. As a client, its the response time. So, both of them are important.

But my point on the script by Nitin is just WHY would I ever like to alter my base table to get the concatenated values. Its not practical. Or would I create a temporary table for the data to get the things done. There are alot of issues involved. Considering 1 million rows example, its not workable.

Plz clear me if I am missing something.


----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sometimes, winning is not an issue but trying.

You can check my BLOG here

Post #949722
Posted Monday, August 2, 2010 5:14 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, August 12, 2014 7:59 AM
Points: 54, Visits: 322
This is some useful code, but I have a bit of a challenge with it. Or should I say "I need it changed a bit".

What if I need another 'level'. like this;

Create Table TestTable (RowID Int Identity(1, 1), Lognumber int, Seq int, HDText varchar(100))

Insert TestTable Values (95224,1,'THIS IS A SUGGESTION. WOULD IT BE POSSIBLE FOR US TO ')
Insert TestTable Values (95224,1,'SYSTEMISE THE WACKY IDEA SYSTEM? IT CAN BASICALLY TAKE ANY FORM ON THE')
Insert TestTable Values (95224,1,'SYSTEM. THE OPERATOR CAN COMPLETE THIS ')
Insert TestTable Values (95224,1,'WITH WHAT IT WAS THAT THEY WANTED TO SEND THE DBA, AND THIS DE- ')
Insert TestTable Values (95224,1,'FAULTS TO THE WACKEY OPERATOR IN EACH OFFICE. THE CORRECT ADDRESS TO ')
Insert TestTable Values (95224,1,'WHERE THE HEADSLAP MUST BE SENT, WILL THEN BE ON THE DB ALONG WITH ALL')
Insert TestTable Values (95224,1,'OTHER DETAIL. THE WACKEY OPERATOR CAN THEN COMPLETE THIS, AND THERE ')
Insert TestTable Values (95224,1,'WILL BE RECORD ON THE SYSTEM OF ALL WACKEYS, WHEN , WHERE AND HOW ')
Insert TestTable Values (95224,1,'THE HEADSLAP HAS BEEN GIVEN TO THE OPERATOR TO AVOID DUPLICATION, AND HEADSLAPS NOT GETTING DELIVERED ')
Insert TestTable Values (95224,4,'Resource changed from TEAM2 ADMIN/SALES to PROJECTS STEERING COMMITTEE')
Insert TestTable Values (95224,4,'Team changed from Team 2 - Admin/Sales/Data Xfer/M to Refer to steering committee)
Insert TestTable Values (95224,13,'Resource changed from PROJECTS STEERING COMMITTEE to THE BIG BOSS')
Insert TestTable Values (95224,13,'Team changed from Refer to steering committee to Team 14 - Management ')
Insert TestTable Values (95224,18,'Team changed from Team 14 - Management to Computer Operations ')
Insert TestTable Values (95224,18,SUGGESTION APPROVED BY Team 14 - Management ')

select * from TestTable

If I need to Recurse by 'Lognumber' then by 'Seq' and then concatenate the text into 1 varchar field, how would I go about this?

Would it also be possible to add a 'Char(10)' at the end of each 'seq' group?

thanks in advance,
Post #962089
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse