SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Multiples address lines on a single row. Pivot?


Multiples address lines on a single row. Pivot?

Author
Message
Danster
Danster
SSC-Enthusiastic
SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)

Group: General Forum Members
Points: 118 Visits: 90
Update...Couldn't wait as this was an urgent request! (Aren't they all).

1st step. Created & saved a view with columns mentioned above, plus an extra LineNumber column.
View is this
SELECT RowID, Address1 + ' ' + Address 2 + ' ' (etc) as DelAddr, row_number() OVER (partition BY RowID
ORDER BY RowID) AS LineNumber
FROM MyTable

2nd Step.
Save another view like this:
SELECT RowID,
MAX(CASE WHEN LineNumber = 1 THEN DelAddr ELSE '' END) as DelAddress1,
MAX(CASE WHEN LineNumber = 2 THEN DelAddr ELSE '' END) as DelAddress2,
MAX(CASE WHEN LineNumber = 3 THEN DelAddr ELSE '' END) as DelAddress3,
MAX(CASE WHEN LineNumber = 4 THEN DelAddr ELSE '' END) as DelAddress4
FROM MyView1
GROUP BY RowID

SWEEEET!
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16890 Visits: 19557
Nice work. I'd miss out the first view though - unless it's going to be used elsewhere, it's simply clutter:

SELECT RowID, 
MAX(CASE WHEN LineNumber = 1 THEN DelAddr ELSE '' END) as DelAddress1,
MAX(CASE WHEN LineNumber = 2 THEN DelAddr ELSE '' END) as DelAddress2,
MAX(CASE WHEN LineNumber = 3 THEN DelAddr ELSE '' END) as DelAddress3,
MAX(CASE WHEN LineNumber = 4 THEN DelAddr ELSE '' END) as DelAddress4
FROM (
SELECT
RowID,
Address1 + ' ' + Address2 + ' ' (etc) as DelAddr,
row_number() OVER (partition BY RowID ORDER BY RowID) AS LineNumber
FROM MyTable
) d
GROUP BY RowID



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search