Solved

SQL - combining multiple records into 1 record

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

751 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