Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL - combining multiple records into 1 record

Posted on 2016-10-25
5
Medium Priority
?
79 Views
Last Modified: 2016-10-26
What I am starting with:
I have a View that brings back the following
InvNbr          CheckNbr
1234              789
1234              678
1234              567

1235              5555

What I want:
a View that would bring back the following using the above view
InvNbr          CheckNbr
1234              789/678/567
1235              5555

Note:  An invoice could have 1 check or as many as 5 checks.

Thanks in advance for your help
LJG
0
Comment
Question by:LJG
5 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 1000 total points
ID: 41858822
There's no easy way to pull this off other than to use FOR XML querying.  Check out T-SQL:  Normalized data to a single comma delineated string and back for an image and code-heavy tutorial on how to pull that off, replacing comma , with slash /
0
 
LVL 4

Assisted Solution

by:Steven Kribbe
Steven Kribbe earned 500 total points
ID: 41858860
It should be done using the for xml trick:

select InvNbr          ,
STUFF(
    (SELECT ', ' + STR(CheckNbr,10,0)
    FROM <yourTable>
    WHERE (InvNbr= T.InvNbr)
    FOR XML PATH(''),TYPE).value('(./text())[1]','VARCHAR(MAX)')
  ,1,2,'') as tmp from <yourTable> T group by InvNbr        
0
 
LVL 20

Assisted Solution

by:Daniel Van Der Werken
Daniel Van Der Werken earned 500 total points
ID: 41858893
This worked for me:

IF OBJECT_ID('tempdb.dbo.#MyTempTable') IS NOT NULL
	DROP TABLE #MyTempTable
CREATE TABLE #MyTempTable(InvNbr INT, CheckNbr INT)
INSERT INTO #MyTempTable (InvNbr, CheckNbr) VALUES (1234, 789)
INSERT INTO #MyTempTable (InvNbr, CheckNbr) VALUES (1234, 678)
INSERT INTO #MyTempTable (InvNbr, CheckNbr) VALUES (1234, 567)
INSERT INTO #MyTempTable (InvNbr, CheckNbr) VALUES (1235, 5555)

SELECT
		MT.InvNbr,
		STUFF((SELECT '/' + CAST(CheckNbr AS VARCHAR(10)) FROM #MyTempTable WHERE InvNbr = MT.InvNbr FOR XML PATH('')), 1, 1, '') AS CheckNbr
FROM #MyTempTable MT

Open in new window


InvNbr	CheckNbr
1234	789/678/567
1234	789/678/567
1234	789/678/567
1235	5555

Open in new window

0
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 41858895
Use DISTINCT if you wanted just the one row per:

SELECT
		DISTINCT
		MT.InvNbr,
		STUFF((SELECT '/' + CAST(CheckNbr AS VARCHAR(10)) FROM #MyTempTable WHERE InvNbr = MT.InvNbr FOR XML PATH('')), 1, 1, '') AS CheckNbr
FROM #MyTempTable MT

Open in new window


There might be better ways, but this gave me the results you listed.
0
 
LVL 2

Author Closing Comment

by:LJG
ID: 41860174
Thanks so much to all of you.  
1) Because of the info from the 3 of you, I got the view I need.
2) This is the first time I've used Stuff XML Path - I  don't understand what's going on (even though it's working), hopefully I can take some time this weekend and learn how to use it on a regular basis.

Thanks again for my education.
LJG
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ready to get certified? Check out some courses that help you prepare for third-party exams.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question