Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Convert rows into Columns in SQL Server, T-SQL

Posted on 2016-09-01
5
Medium Priority
?
140 Views
Last Modified: 2016-09-13
Hi Guys,
I need to convert rows into columns. My requirement is that a postcode can have several addresses like below.

AB1 2DE , Address 1
AB1 2DE, Address 2
AB1 2DE, Address 3
.................................
.................................
AB1 2DE, Address N
AB2 1DE, Address 1
''''''''''''''''''''''''''''''''''''''''
AB2 1DE, Address N

I want to display all the addresses of a postcode as columns
AB12DE Address1 Address2 Address3 .........................Address N
AB2 1DE Address1 Address2 Address3 .........................Address N

Please note i am using SQL Server 2014

Kindest regards
0
Comment
Question by:shah36
[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
  • 3
  • 2
5 Comments
 
LVL 49

Accepted Solution

by:
PortletPaul earned 2000 total points
ID: 41779706
ALL addresses of a postcode as columns......
What is the maximum number of addresses you have to deal with?

select max(n) as maxnum
from (
    select postcode, count(*) as n from yourtable group by postcode
   ) d

Only if that number is reasonably small should you "pivot" rows into columns
i.e. if maxnum is hundreds or thousands would you really want to try it?

You need "dynamic sql" to achieve what you want, e.g.

DECLARE @cols AS nvarchar(max)
DECLARE @query AS nvarchar(max)

SET @cols = STUFF((
      SELECT DISTINCT
            ',' + QUOTENAME(address)
      FROM yourtable
      FOR xml PATH (''), TYPE
)
.value('.', 'NVARCHAR(MAX)')
, 1, 1, '')

SET @query = 'SELECT postcode, ' + @cols + ' FROM
                (
                    SELECT
                          postcode, address
                     FROM yourtable
               ) sourcedata
                pivot
                (
                     max([address])
                    FOR [address] IN (' + @cols + ')
                ) p '

--SELECT @query /* use select to inspect the generated sql */

-- EXECUTE(@query) /*once satisfied that sql is OK, use execute (INSTEAD) */

Open in new window

0
 

Author Comment

by:shah36
ID: 41786118
Thanks a lot it helps
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 41786961
Great! Will you close off the question please?
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 41795130
Thank you. Closure of questions is really appreciated.

Cheers
Paul
0
 

Author Comment

by:shah36
ID: 41796034
You are welcome Paul and really sorry i did not realise that i had not closed the question.

regards
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

604 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