在SQL中替换和分组

时间:2010-07-22 21:48:19

标签: 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 个答案:

答案 0 :(得分: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

如果你只是想在取消'/'之后取回一个URL,即使两个是等效的,你也可以这样做

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

但是你无法控制哪些“匹配”被删除。