SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Is this Pivot? Unpivot? or what?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Bill_C
Constraint Violating Yak Guru

United Kingdom
291 Posts

Posted - 04/10/2013 :  05:16:12  Show Profile  Reply with Quote
I have a table that I want to copy data from into another table, the table I want to copy data from has data in the form of:-

Field1 | Field2 | Field3 | Field4

d1 | one | T1 | C1
d1 | one | T1 | C2
d1 | one | T1 | C3
d1 | one | T2 | C1
d1 | two | T2 | C2
d1 | two | T2 | C3
d1 | three | T1 | C2
d1 | three | T1 | C3
d2 | one | T1 | C1
d2 | one | T1 | C2
d2 | one | T2 | C1
d2 | one | T2 | C2
d3 | one | T3 | C1
d3 | one | T3 | C2
d3 | one | T3 | C3

I would like to copy over the data to the new table so that it looks like this:-

Field1 | Field2 | Field3 | Field4

d1 | one | T1 | C1,C2,C3
d1 | two | T2 | C2,C3
d1 | three | T1 | C2,C3
d2 | one | T2 | C1,C2
d3 | one | T3 | C1,C2,C3


There are about 800,000 records in the table and Field1, Field2 and Field3 hold many different combinations with lots of entries for Field4 for each (not just the C1,C2,C3 I've used in the example above)

Is there a quicker way of doing this other than using a cursor as the cursor takes a couple of hours to run?

B





Edited by - Bill_C on 04/10/2013 05:25:24

Bill_C
Constraint Violating Yak Guru

United Kingdom
291 Posts

Posted - 04/10/2013 :  05:24:35  Show Profile  Reply with Quote
Sorry, had to add pipes to try and break up the field names and data.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 04/10/2013 :  05:31:06  Show Profile  Reply with Quote
None. its called rowset concatenation

see


SELECT t.*,
STUFF((SELECT ',' + Field4 FROM Table WHERE Field1 = t.Field1 AND Field2 = t.Field2 AND Field3 = t.Field3 ORDER BY Field4 FOR XML PATH('')),1,1,'')
FROM (SELECT DISTINCT Field1,Field2,Field3 FROM Table)t


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Bill_C
Constraint Violating Yak Guru

United Kingdom
291 Posts

Posted - 04/10/2013 :  05:47:10  Show Profile  Reply with Quote
Thank you for your help, that works perfectly.

B
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 04/10/2013 :  05:50:00  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000