I have a table like this:
218 4 AudioVerse https://www.audioverse.org/english/podcasts/latest Latest NULL 2012-03-29 15:32:44.287
222 7 TPB http://rss.thepiratebay.se/0 Alt NULL 2012-03-31 17:55:49.223
223 7 EZTV http://www.ezrss.it/feed/ Alt NULL 2012-03-31 17:56:41.573
226 11 The Piratebay http://rss.thepiratebay.se/100 Audio Only NULL 2012-04-04 14:57:45.377
227 11 The Piratebay http://rss.thepiratebay.se/200 Video Only NULL 2012-04-04 14:58:04.650
229 15 ThePirateBay http://rss.thepiratebay.se/200 NULL 2012-04-06 22:40:12.730
230 14 The Pirate Bay http://rss.thepiratebay.se/0 NULL 2012-04-08 00:59:13.217
232 14 AudioVerse https://www.audioverse.org/english/podcasts/latest NULL 2012-04-08 01:03:22.787
233 14 EZTV http://www.ezrss.it/feed/ NULL 2012-04-08 01:20:55.860
234 17 Twit http://twit.tv/node/feed NULL 2012-04-13 18:59:23.037
235 17 Diggnation http://revision3.com/diggnation/feed/MP4-Large Video Large NULL 2012-04-13 19:01:52.817
I want a query or stored procedure to return a table that returns distinct, AND a column with which id's that has this url in common, (The second id column (4,7,11,15,14,17) is the id's in question) in comma-separated form like this:
http://rss.thepiratebay.se/0 3 4,5,7,7,11,11,15,14,14,14,17,17
http://rss.thepiratebay.se/200 2 4,5,7,7,11,11,15,14,14,14,17,17
http://www.ezrss.it/feed/ 2 4,5,7,7,11,11,15,14,14,14,17,17
https://www.audioverse.org/english/podcasts/latest 2 4,5,7,7,11,11,15,14,14,14,17,17
http://revision3.com/diggnation/feed/MP4-Large 1 4,5,7,7,11,11,15,14,14,14,17,17
http://twit.tv/node/feed 1 4,5,7,7,11,11,15,14,14,14,17,17
http://rss.thepiratebay.se/100 1 4,5,7,7,11,11,15,14,14,14,17,17
Here the first column is the distinct url, the next is how many times the url is present (differs from the main table above, but you get the point), and a comma-separated string of which id's that these urls has in common.
The comma-separated string in this case is not correct.
The queries I used to create this table is:
DECLARE @listStr VARCHAR(MAX)
SELECT @listStr = COALESCE(@listStr+',' ,'') + cast(userid as varchar)
FROM sites
select url, COUNT(*), (@listStr)
from sites
group by url
order by COUNT(*) desc
I am using SQL Server 2008 R2.
The question I present to you is: how to do this, and which way (if there is more than one) is most efficient?
Any help would be appreciated. I could do this in C# code, but I'd much rather have it in either a stored procedure, or a query, which ever is easiest, and/or faster :P