Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL - combining multiple records into 1 record

Posted on 2016-10-25
5
Medium Priority
?
75 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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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…

660 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