SQL Clone
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
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1706 Visits: 608
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 Guru
SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)

Group: General Forum Members
Points: 91672 Visits: 38953
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
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1706 Visits: 608
Interesting. Thanks!
raym85
raym85
SSC-Addicted
SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)

Group: General Forum Members
Points: 450 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
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1706 Visits: 608
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