Skip to main content

Concatenating row values in a single statement using CROSS APPLY

One more SQL issue, one more solution provided. I feel good about it now ;). I am adding it to my collection because it really helps in reducing the duration to execute the process. It is about a new feature which was introduced way back in SQL 2005. CROSS APPLY.

The Scenario: Let me lay down the scenario, I have a table which has some 100 thousand + records. This is a very (deliberately) de normalized table. The structure and contents of the table are as below.




IDFirstNameEducation
1Vighnesh
BSC
KUD
1995


2Vighnesh


MSC
KUD
1997

3Vighnesh

PHD
MIT
2009

4Mark


BCOM
KUD
2000

5Mark
 
MBA
IIMB
2008

6Vicky


BE
VIT
2008




From the above table, I want to get only distinct records, with the education column concatenated. Something like this…



ID

FirstName

Education

1

Vighnesh

 BSCKUD
1995
MSC
KUD1997
PHDMIT2009

4

Mark


BCOMKUD
2000
MBAIIMB2008


6

Vicky


BE
VIT2008




This can be achieved with a simple query…


SELECT DISTINCT FirstName, CAST(Concat_Education as nvarchar(max)) as Education_ctn
FROM EducationHistory p1
CROSS APPLY
(
SELECT cast(Education + '' as xml)
FROM EducationHistory p2
where p2.Name = p1.Name
ORDER BY Education
FOR XML PATH(''), TYPE
)
V ( Concat_Education )


In the above query we are using FOR XML PATH(‘’), TYPE. If we don’t use this along with casting the Education column as xml (cast(Education + '' as xml)), we will get the output xml as “<” and “>”.
While searching for the possible solution, I searched the web with different approaches. This also shows the evolution in my thinking from start to end. Sample this:
1. assigning value to variable in sub query
2. can I use variable in sub query?
3. coalesce in sql server
4. combine value of 2 rows sql
5. combine value of 2 rows sql in DISTINCT query
6. concat subquery rows
7. concat using coalesce in subquery – here is where I got the answer (thanks to this blog)
8. FOR XML PATH('') < >
9. FOR XML PATH('') < with < get rid
10. FOR XML PATH('') is replacing < with <
11. FOR XML PATH('') sql server

Feel free to contact me in case you need help.
-Vighnesh Bendre

Comments

Popular posts from this blog

Working with large xml files in c# .net

Working with large (huge) xml files is always a pain in the … The reason? These files can’t be loaded in to memory. On my desktop, where I have 2 gigs memory, I can’t open the file in even notepad. I was presented with a challenge recently to manipulate one such large xml file. The xml file was of 550+ MB. I know many would say I have seen bigger xml files than this. But the heart of the matter is if I can’t open 550+ MB file in notepad or in xmldocument in c#, then I can’t open any file bigger than this. And hence the logic to play with these files would remain same. The scenario: We have an xml file from which we want to remove a single node without removing its children. In the below sample xml fragment, the node has to be removed. The children nodes, must then be attached to ( node’s parent) node. One Two 100.22 GoodDay 3 4 Five 200.09 Cra...

Upgrade and Migration for SharePoint Foundation 2010

      1.1 Introduction Microsoft SharePoint Foundation 2010 has been designed for scale and performance and as such requires new hardware and software requirements. There are 3 major steps while upgrading. 1. Plan and Prepare 2. Perform a database attach upgrade 3. Verify upgrade 1.2 Plan and Prepare   Before we run any process to upgrade from Windows SharePoint Services 3.0 to Microsoft SharePoint Foundation 2010, we have to determine which upgrade approach to take. In our scenario, Database Attach Upgrade seems to be appropriate approach to follow. We can upgrade the content for the environment on a separate farm. The result is that you do not upgrade any of the services or farm settings. You can upgrade the databases in any order and upgrade several databases at the same time. While each database is being upgraded, the content in that database is not available to users. 1.2.1 Upgrade Approach A database attach upgrade enables you to move to...

Object Oriented Analysis & Design (OOAD) and Unified Modelling Language (UML)

Part 1 – Identifying Use Cases – Use Case Diagrams Recently I went through OOAD and UML training. The OOAD and UML tutorial was very impressive and I decided to share it with you. Object Oriented Analysis & Design and Unified Modelling Language is very important in a life cycle of a project. Previously I was involved in project requirement study and technical design. But this time, I learned the tricks of the trade. I discovered different tips for identifying Use Cases, Actors and Classes . In this series of posts, I am planning to take you through the process of involvement of UML in Requirement analysis and Design phase. This series will include 3 parts... Part 1. Identifying Use Cases – Use Case Diagrams Part 2. Realizing Use Cases – Sequence Diagrams Part 3. Identifying Classes – Class Diagrams For this purpose we will take commonly available sample requirement – Student Registration process. From this requirement we will identify the ACTORS and US...