2010-07-22 74 views
2

您好我希望通過替換字符的字段對結果進行分組,但也保留原始值。SQL中的替換和分組依據

這是我有,但我需要返回原始的URL值,而不會改變結果。

select Count(ID) as TotalClicks, Replace(URL, '/', '') as DistinctURL 
    from email_click_throughs 
where emailid = @ID 
group by Replace(URL, '/', '') 
order by TotalClicks desc 
+1

但大概可能有多個原始URL映射到distinctURL,那麼您希望如何處理?例如URL =「/default.html」和URL =「default.html」。你還使用了什麼RDBMS? – 2010-07-22 21:50:33

回答

3

這會給你你所描述的,但它是在分組「替換(URL ......」多餘的,所以我可能會誤解:

select Count(ID) as TotalClicks, URL as DistinctURL 
from email_click_throughs 
where emailid = @ID 
group by Replace(URL, '/', ''), URL 
order by TotalClicks desc 

如果你只是想獲得回一個網址,即使兩者進行去除後「/」相當於,你可以不喜歡

select Count(ID) as TotalClicks, Max(URL) as DistinctURL 
from email_click_throughs 
where emailid = @ID group by Replace(URL, '/', '') 
order by TotalClicks desc 

但在這你「匹配」的是越來越下降了,你沒有太多的控制權。

+0

在第二個聲明中指明瞭它,謝謝 – Jamie 2010-07-22 22:03:29