sql server - comma separated names based on company ID -


i have following table of employee details

employeename   compayid    companylastactive --------------------------------------------------------- robort          112        10 jun 2015 09:30  john            113        11 jun 2015 11:10 sunny           114        14 jun 2015 16:10 sumanth         114        15 jun 2015 18:11 usha            115        07 jun 2015 13:14 sudheer         115        14 jun 2015 17:10 sweety          115        08 jun 2015 16:34 

i need latest employee active time based on companyid comma separated employeename below

employeename           compayid        companylastactive --------------------------------------------------------- robort                 112               10 jun 2015 09:30  john                   113               11 jun 2015 11:10 sunny, sumanth         114               15 jun 2015 18:11         usha, sudheer, sweety   115               14 jun 2015 17:10 

please me how solve.

select employeename = stuff((           select ',' + e1.employeename           dbo.employee e1           e.compayid = e1.compayid           xml path(''), type).value('.', 'nvarchar(max)'), 1, 1, ''),         e.compayid,         max(companylastactive) companylastactive dbo.employee e group e.compayid order e.compayid 

result:

employeename        compayid    companylastactive ------------------------------------------------------- robort              112         june, 10 2015 09:30:00 john                113         june, 11 2015 11:10:00 sunny,sumanth       114         june, 15 2015 18:11:00 usha,sudheer,sweety 115         june, 14 2015 17:10:00 

sample result in sql fiddle.


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -