在sql查询中过滤

时间:2012-07-17 08:24:25

标签: sql-server sql-server-2005

我的样本表数据是:

Table Data

我的SQL查询是:

SELECT
Distinct
   CASE CHARINDEX( '?', SessionURL)
       WHEN 0 THEN SessionURL
       ELSE LEFT(SessionURL, CHARINDEX( '?', SessionURL) - 1) END AS VisitPage, count(*) as [VisitTime] 
FROM SessionTracker  
 group by SessionURL
order by [VisitTime] desc

查询运行成功并返回一个不带查询字符串的值。上述数据的结果为:

result
但我只想要一种格式的结果:

Default
Modules

在sql查询中是否有可能使用这种类型的过滤器。如果是建议的话。谢谢。

3 个答案:

答案 0 :(得分:0)

是。与过滤查询字符串相同,过滤/和。您需要SUBSTRING功能。要查找/的最后一次出现,请使用REVERSE函数。

select pagename, COUNT(*)
from
(
    select 
        reverse(case CHARINDEX('/', withoutsuf) when 0 then withoutsuf 
        else LEFT(withoutsuf,CHARINDEX('/', withoutsuf)-1 ) end) as pagename
    from
    (
        select 
            substring(withoutq, CHARINDEX('.', withoutq)+1, LEN(withoutq)) withoutsuf
        from
        (
            select
                REVERSE(
                    CASE CHARINDEX( '?', SessionURL) 
                    WHEN 0 THEN SessionURL 
                    ELSE LEFT(SessionURL, CHARINDEX( '?', SessionURL) - 1) END) as withoutq
            from SessionTracker
        ) v
    ) v
) v
group by pagename

答案 1 :(得分:0)

试试这个

declare @Path varchar(500)='http://somename/some_name.aspx'
SELECT  replace(substring(@Path,len(@Path)-
CHARINDEX('/',REVERSE(@Path))+2,len(@Path)),'.aspx','')

答案 2 :(得分:0)

declare @t table(path varchar(100))
insert into @t (path)
select 'http://localhost:58588/default.aspx'  union all
select 'http://localhost:58588/default.aspx'  union all
select 'http://localhost:58588/super-user/modules.aspx'  

select 
    distinct replace(right(path,charindex('/',reverse(path))-1),'.aspx','') 
from 
    @t