Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


A Pivot, but not exactly.


A Pivot, but not exactly.

Author
Message
TheGreenShepherd
TheGreenShepherd
SSChasing Mays
SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)

Group: General Forum Members
Points: 600 Visits: 591
I have a table that could look like the following:

ID FK_ID Value
1 100 'Blue'
2 100 'Black'
3 100 'Green'
4 101 'Blue'
5 101 'Green'
6 102 'Black'

I need a query that outputs the following:

FK_ID NewVal
100 'Blue,Black,Green'
101 'Blue, Green'
102 'Black'

Also, I won't always know that 'Blue', 'Black', or 'Green' are going to be the distinct values.
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24179 Visits: 37948
Use something like this:



with basedata as (
select distinct
FK_ID
from
dbo.MyTable
)
select
FK_ID,
NewVal = stuff((select ',' + Value
from dbo.MyTable mt
where mt.FK_ID = bd.FK_ID
order by mt.ID
for xml path(''),TYPE).value('.','varchar(max)'),1,1,'')
from
basedata bd;




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
TheGreenShepherd
TheGreenShepherd
SSChasing Mays
SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)

Group: General Forum Members
Points: 600 Visits: 591
Interesting. Thanks!
raym85
raym85
Old Hand
Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)

Group: General Forum Members
Points: 376 Visits: 198
the method above is useful since its dynamic, but i wanted to show you a more manual way of doing it as well.



CREATE taBLE #TEMP
(
id INT,
fk_id INT,
value VARCHAR(10)


)
INSERT INTO #TEMP
VALUES(1, 100, 'Blue'),
(2, 100, 'Black'),
(3, 100, 'Green'),
(4, 101, 'Blue'),
(5, 101, 'Green'),
(6, 102, 'Black')

SELECT FK_ID, REPLACE(REPLACE(RTRIM(LTRIM(COALESCE(Blue ,'') +' '+ COALESCE(Black,'') +' '+ COALESCE(Green ,''))),' ',','),',,',',')AS VALUESS

FROM(
select fk_id, MIN(case when value = 'Blue' then 'Blue' END) Blue,MIN(case when value = 'Black' then 'Black' END) black, MIN(case when value = 'Green' then 'Green' END)Green
from #TEMP
group by fk_id)I
TheGreenShepherd
TheGreenShepherd
SSChasing Mays
SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)

Group: General Forum Members
Points: 600 Visits: 591
Thanks, but like I mentioned - "I won't always know that 'Blue', 'Black', or 'Green' are going to be the distinct values."
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