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

Combining fields of matching records Expand / Collapse
Author
Message
Posted Wednesday, October 2, 2013 11:16 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, August 29, 2014 5:10 AM
Points: 100, Visits: 314
I have a table that contains several fields, and some of the records are identical except for one of those fields. I'm trying to write a query to return a single row that contains each field plus a combination field representing the concatenated values that were unique to each row.

For example:

FIELD1 | FIELD2 | FIELD3 | FIELD4
ROW1 | ABC DEF GHI JKL
ROW2 | ABC DEF GHI MNO

And I want the output to look like:

OUTPUT | ABC DEF GHI JKL,MNO

I can do this in .NET code, but I was hoping to do this in the SQL. Is it possible?

I've found this example, but haven't been able to make it work correctly and don't really understand all of it:

SELECT p.ASSIGNNUM,p.DESC,p.STARTDATE,LEFT(el.EmpList,LEN(el.EmpList)-1)
FROM TableParent p
CROSS APPLY (SELECT EMPLOYEENUM + ',' AS [text()]
FROM TableChild
WHERE ASSIGNNUM =p.ASSIGNNUM
FOR XML PATH(''))el(EmpList)

Post #1500860
Posted Wednesday, October 2, 2013 12:55 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 2:34 PM
Points: 12,923, Visits: 12,342
It is absolutely possible to do in sql. This article explains how to generate comma separated lists.

http://www.sqlservercentral.com/articles/71700/

If you need coding assistance, we will need a few things:

1. Sample DDL in the form of CREATE TABLE statements
2. Sample data in the form of INSERT INTO statements
3. Expected results based on the sample data

Please take a few minutes and read the first article in my signature for best practices when posting questions.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1500888
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse