在表web中,字段url中,我想计算每天记录出现www.htmldata.cn这个字符串出现的次数,如果一次没有出现就显示 no this url,这个怎么做呢?
解决方案 »
- 发番的问题 求SQL
- 问个概念性的问题!
- 奇怪问题。我搬家后。把电脑打开,发现Sql server 2000不能用了
- 累加排序老问题!
- 无法删除主键约束时候报错,请问如何解决
- 关于‘本期’‘对比期’‘当日’取数据问题
- 怎样设定在本机打开sql server2005企业管理器也要密码?
- 求助:sql语句的写法~~~
- 存储过程中调用如下语句 execute immediate 'create global temporary table test (a char(1))' 提示权限不够,为什么?
- 如何将 TXT 文本的内容导入数据库? 急!
- 多表联合查询,是不是效率很低、速度很慢?
- sqlserver 2005 的varchar(max) 怎么多余8000会出错
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
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 行)