在表web中,字段url中,我想计算每天记录出现www.htmldata.cn这个字符串出现的次数,如果一次没有出现就显示 no this url,这个怎么做呢?

解决方案 »

  1.   

    declare @url varchar(100)
    set @url = 'www.htmldata.cn'
    select 
    case 
        when exists(select 1 from web where url like '%' + @url + '%')
        then cast((select count(*) from web where url like '%' + @url + '%') as varchar)
        else 'no this url'
    end
      

  2.   

    --是否这样?
    create table web(url varchar(50),dt datetime)
    insert web select 'www.htmldata.cn','2007-06-06 14:26'
    union all select 'www.htmldata.cn','2007-06-06 14:28'
    union all select 'www.ht.com','2007-06-06 14:28'
    union all select 'www.htmldata.cn','2007-06-07 14:28'
    union all select 'www.htmldata.com','2007-06-07 14:29'
    union all select 'www.htmld.cn','2007-06-08 14:29'
    union all select 'www.html.cn','2007-06-08 15:29'
    union all select 'www.htmld.cn','2007-06-09 14:31'
    union all select 'www.htmldata.cn','2007-06-09 15:29'
    union all select 'www.htmldata.cn','2007-06-09 16:29'select 日期,次数=case when 次数>0 then cast(次数 as varchar) else 'no this url' end from
    (
    select 日期,次数=sum(case 次数 when 1 then 1 else 0 end) from
    (
    select 日期=convert(char(10),dt,120),url,
    次数=case when exists(select 1 from web where convert(char(10),dt,120)=convert(char(10),a.dt,120) 
    and a.url='www.htmldata.cn') then 1 else 0 end
    from web a
    )b
    group by 日期
    )cdrop table web日期         次数                             
    ---------- ------------------------------ 
    2007-06-06 2
    2007-06-07 1
    2007-06-08 no this url
    2007-06-09 2(所影响的行数为 4 行)