Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Concatenation maybe?

Author  Topic 

chrispy
Posting Yak Master

107 Posts

Posted - 2007-11-18 : 10:42:56
Trying to come up with a solution with this one and not even sure what to search for. So here I am to asking in the forums.

I have a query that returns such :


ID State City
---- ----- ----
A MA Boston
A NY New York City
A OK Tulsa
B MA Boston
B FL Miami
C GA Atlanta



What I would like to do is combine the rows with the same ID as such


ID State1 State2 State3 City1 City2 City3
---- ----- ------- ------ ----- ----- -----
A MA NY OK Boston New York Tulsa
B MA FL null Boston Miami null
C GA null null Atlanta null null



I think that should simply give an idea of what I am trying to do.
More or less looking for a point in the right direction.

Any help would greatly appreciated.

Thanks,
Chris

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2007-11-18 : 11:13:12

http://weblogs.sqlteam.com/jeffs/archive/2005/05/02/4842.aspx
http://weblogs.sqlteam.com/jeffs/archive/2005/05/15/5175.aspx
http://weblogs.sqlteam.com/jeffs/archive/2005/05/11/5101.aspx


Those are a start.



Poor planning on your part does not constitute an emergency on my part.

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-11-18 : 21:10:16
quote:
Concatenation maybe?

No. PIVOTation

DECLARE @sample TABLE
(
[ID] CHAR(1),
State CHAR(2),
City varchar(15)
)
INSERT INTO @sample
SELECT 'A', 'MA', 'Boston' UNION ALL
SELECT 'A', 'NY', 'New York City' UNION ALL
SELECT 'A', 'OK', 'Tulsa' UNION ALL
SELECT 'B', 'MA', 'Boston' UNION ALL
SELECT 'B', 'FL', 'Miami' UNION ALL
SELECT 'C', 'GA', 'Atlanta'

;WITH sample_data ([ID], State, City, SeqNo) AS
(
SELECT [ID], State, City,
SeqNo = row_number() OVER (PARTITION BY [ID] ORDER BY [ID], State)
FROM @sample
)
SELECT [ID] = coalesce(s.[ID], c.[ID]),
state_1, state_2, state_3, city_1, city_2, city_3
FROM
(
SELECT [ID], state_1 = p.[1], state_2 = p.[2], state_3 = p.[3]
FROM (
SELECT [ID], SeqNo, State
FROM sample_data
) s
pivot
(
MAX(s.State)
FOR s.SeqNo IN ([1], [2], [3])
) p
) s
FULL OUTER JOIN
(
SELECT [ID], city_1 = p.[1], city_2 = p.[2], city_3 = p.[3]
FROM (
SELECT [ID], SeqNo, City
FROM sample_data
) c
pivot
(
MAX(c.City)
FOR c.SeqNo IN ([1], [2], [3])
) p
) c ON s.[ID] = c.[ID]

/*
ID state_1 state_2 state_3 city_1 city_2 city_3
---- ------- ------- ------- --------------- --------------- ---------------
A MA NY OK Boston New York City Tulsa
B FL MA NULL Miami Boston NULL
C GA NULL NULL Atlanta NULL NULL
*/



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2007-11-19 : 01:10:01
Khatan,

I am just curious why would you use a CTE over a variable/tmp table?

Thanks.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-11-19 : 09:01:41
Because i don't want to create a permanent table in my database. The table variable is just for testing, demonstration, illustration of the query. Of-course OP should replace the table variable with the actual table name and column name.


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2007-11-19 : 14:04:23
Oh... I was just curious why a cte was used in this example over a #tmp, or @tmp table, I thought there might have been another reason.

Thanks for clarifying.
Go to Top of Page
   

- Advertisement -