April 14, 2015 at 5:02 am
hi,
i have a query i.e.
select project, description from table
with results
chocolate | white
chocolate | black
chocolate | brown
sugar | black
vinegar | yellow
i would like to get a result like:
chocolate | white, blakc, brown
sugar | black
vinegar | yellow
so I would like to get all values of one project in one records included with separators/a space in it
April 14, 2015 at 5:24 am
Use FOR XML PATH('') .See http://stackoverflow.com/questions/4944680/flatten-sql-server-table-to-a-string"> http://stackoverflow.com/questions/4944680/flatten-sql-server-table-to-a-string
for example.
April 14, 2015 at 5:29 am
Create table test
(project varchar(50),
descripation varchar(50)
)
Insert test
SELECT 'chocolate' ,'white'
UNION ALL
SELECT 'chocolate' ,' black'
UNION ALL
SELECT 'chocolate' ,' brown'
UNION ALL
SELECT 'sugar' ,' black'
UNION ALL
SELECT 'vinegar' ,'yellow'
SELECT DISTINCT project,
STUFF( (SELECT ',' + descripation FROM test WHERE Test.project = T.project FOR XML PATH('')) ,1,2,'')
FROM test T
April 14, 2015 at 5:39 am
i am getting the error
`the multipart identifier project could not be found`
April 14, 2015 at 5:44 am
bauke.dijkstra (4/14/2015)
i am getting the error`the multipart identifier project could not be found`
Without your code it is hard to give an answer.
April 14, 2015 at 5:48 am
Create table bstest
(project varchar(50),
description varchar(50)
)
Insert bstest (project, description)
(select project, description from table)
** insert 19 rows **
SELECT DISTINCT project,
STUFF( (SELECT ',' + description FROM bstest WHERE bstest.project = T.project FOR XML PATH('')) ,1,2,'')
FROM bstest T
April 14, 2015 at 5:51 am
bauke.dijkstra (4/14/2015)
Create table bstest(project varchar(50),
description varchar(50)
)
Insert bstest (project, description)
(select project, description from table)
** insert 19 rows **
SELECT DISTINCT project,
STUFF( (SELECT ',' + description FROM bstest WHERE bstest.project = T.project FOR XML PATH('')) ,1,2,'')
FROM bstest T
So you have a table named "table"? (select project, description from table)
April 14, 2015 at 5:51 am
i think i changed something. now it is working thanks
April 14, 2015 at 5:53 am
Removed
-- Gianluca Sartori
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply