Solved

SQL - combining multiple records into 1 record

Posted on 2016-10-25
5
33 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 65

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 19

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 19

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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 set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now