[英]SQL query or procedure with distinct and comma-separated values for each distinct row
我有一張這樣的桌子:
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
我希望查詢或存儲過程返回一個表,該表返回不重復的值,並且返回一個具有與之相同的URL的ID的列(第二個ID列(4,7,11,15,14,17)是該ID所在的問題),以逗號分隔的形式是這樣的:
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
這里的第一列是不同的url,第二列是該URL出現的次數(與上面的主表不同,但您可以理解這一點),以及以逗號分隔的字符串,這些URL的ID相同。
在這種情況下,逗號分隔的字符串不正確。
我用來創建此表的查詢是:
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
我正在使用SQL Server 2008 R2。
我要向您提出的問題是:該怎么做?哪種方法(如果有多個)最有效?
任何幫助,將不勝感激。 我可以用C#代碼做到這一點,但我寧願將其存儲在存儲過程或查詢中,這是最簡單和/或更快的:P
我使用合並方法。 我發現,結果串聯的字符串取決於在使用COALESCE之前是否已使用值聲明了該字符串。 以下腳本演示了這一點:
DECLARE @Mystring nvarchar( 500 ); --declare but do not set inital value
CREATE TABLE dbo.mytable( id int IDENTITY(1 , 1) PRIMARY KEY , name nvarchar( 40 ));
INSERT INTO mytable( name )
VALUES( 'Peter' ) , ( 'Paul' ) ,( 'Mary' );
SELECT @Mystring = COALESCE( @Mystring + ',' , '' ) + name
FROM dbo.mytable
ORDER BY id;
PRINT @Mystring; -- will print 'Peter,Paul,Mary' with no leading comma
SET @Mystring = ''; --now set to initial empty string
SELECT @Mystring = COALESCE( @Mystring + ',' , '' ) + name
FROM dbo.mytable
ORDER BY id;
PRINT @Mystring; --will print ',Peter,Paul,Mary' ie with leading comma
我不知道為什么tsql會這樣做(我正在使用2008 R2 Express),但是值得一提。 現在,我將字符串變量初始化為空字符串,並使用SUBSTRING()函數刪除前導定界符以確保行為一致。
我對tsql還是很陌生,所以很抱歉,如果這是舊帽子的東西,但是我沒有在其他地方找到它。
嘗試這個:
select url, count(*), group_concat(userid)
from sites
group by url
order by COUNT(*) desc
@BjørnØyvindHalvorsen
這個問題本來就很古老,但是遇到了這個問題,並想到了一些啟發。
---創建表
創建表test_kin(id int,location varchar(max),url varchar(max))轉到
---插入一些值
插入test_kin值(4,'AudioVerse','https://www.audioverse.org/english/podcasts/latest')
插入test_kin值(7,'TPB','http://rss.thepiratebay.se/0')
插入test_kin值(7,'EZTV','http://www.ezrss.it/feed/')
插入test_kin值(11,'The Piratebay','http://rss.thepiratebay.se/100')
插入test_kin值(11,'The Piratebay','http://rss.thepiratebay.se/200')
插入test_kin值(15,'ThePirateBay','http://rss.thepiratebay.se/200')
插入test_kin值(14,“海盜灣”,“ http://rss.thepiratebay.se/0”)
插入test_kin值(14,'AudioVerse','https://www.audioverse.org/english/podcasts/latest')
插入test_kin值(14,'EZTV','http://www.ezrss.it/feed/')
插入到test_kin值中(17,'Twit','http://twit.tv/node/feed')
插入到test_kin值中(17,'Diggnation','http://revision3.com/diggnation/feed/MP4-Large')
-查詢產生結果
select distinct url
,count(*) as ID
,stuff((
select ',' + cast([id] as varchar(max))
from test_kin
for xml path('')
), 1, 1, '') as Result
由test_kin組按URL順序按ID
HTH,健
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.