Solved

SQL - combining multiple records into 1 record

Posted on 2016-10-25
5
70 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 250 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 125 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 125 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

622 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