sql server - How to see the actual values while grouping by instead of count in sql -


i have table in sql server 2008 this:

id   name ------------ 1    jack 2    john 3    maria 4    jack 5    jack 6    john 

i trying see ids having same name in 1 column.

select count(id), name mytable group name 

the query above giving me number of ids having same name. see is:

    id    name     ------------     1,4,5 jack     2,6   john     3     maria 

how can provide this? thanks

declare @yourtable table (id int, name varchar(50)) insert @yourtable values (1,'jack'), (2,'john'), (3,'maria'), (4,'jack'), (5,'jack'), (6,'john')  select name       ,ids  = stuff((select distinct ','+cast(id varchar(25))                        @yourtable                       name=a.name                      xml path ('')),1,1,'')    (select distinct name @yourtable ) 

returns

name    ids jack    1,4,5 john    2,6 maria   3 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -