错在哪儿啦?为什么会都出现在COUNT(0) =2或3时的查询结果里?
USE TEST
GO
if object_id('tempdb.dbo.#tb1') is not null drop table #tb1
go
create table #tb1 (Rid [int] IDENTITY(1,1) NOT NULL,SRN INT,no1 int,no2 int,no3 int,no4 int,no5 int,notext varchar(20))
insert #tb1
select '258131','09','22','31','33','34','09 22 31 33 34' union all
select '258132','09','22','31','33','35','09 22 31 33 35' union all
select '258133','09','22','31','34','35','09 22 31 34 35' union all
select '258134','09','22','32','33','34','09 22 32 33 34' union all
select '258135','09','22','32','33','35','09 22 32 33 35' union all
select '258136','09','22','32','34','35','09 22 32 34 35' union all
select '258137','09','22','33','34','35','09 22 33 34 35' union all
select '258138','09','23','24','25','26','09 23 24 25 26' union all
select '258139','06','23','24','25','27','06 23 24 25 27' union all
select '258140','09','23','24','25','28','09 23 24 25 28'
if object_id('tempdb.dbo.#tb2') is not null drop table #tb2 --数据量超大就用了临时表#tb2,没有用cte建临时表.
go
select *,ROW_NUMBER() over(PARTITION by rid order by val desc) as sn
into #tb2
from #tb1
unpivot(val for col in (no1,no2,no3,no4,no5)) p
if object_id('tempdb.dbo.#tb3') is not null drop table #tb3
go
select Rid,col,val%10 as val
,ROW_NUMBER() over(partition by rid order by val%10) as vid
into #tb3
from #tb2
select * from (
select Rid
from #tb3
group by Rid,val-vid having(COUNT(0) between 2 and 2 ) -- 后期需要所以不要用 having(COUNT(0)>n 的格式。
) a
left join #tb1 b on a.Rid=b.Ridhaving(COUNT(0) between 2 and 2) ,是否可以理解为:提取no1,no2,no3,no4,no5个位上的数,有2个连续的自然数
查询结果为
having(COUNT(0) between 3 and 3) ,是否可以理解为:提取no1,no2,no3,no4,no5个位上的数,有3个连续的自然数
查询结果为
having(COUNT(0) between 4 and 4) ,是否可以理解为:提取no1,no2,no3,no4,no5个位上的数,有4个连续的自然数
查询结果为
having(COUNT(0) between 5 and 5) ,是否可以理解为:提取no1,no2,no3,no4,no5个位上的数,有5个连续的自然数
查询结果为
问题描述:
1/为什么当COUNT(0) =2或3 不同时,查询结果里有相同的记录09 23 24 25 28 ?
2/查询的记录09 23 24 25 28的个位是3-4-5连续3个的自然数,而不是2个连续的自然数,为什么当COUNT(0) =2时会查询出这样的记录?
3/既然查询的记录09 23 24 25 28的个位是3-4-5连续3个的自然数,而不是2个连续的自然数,为什么会出现在COUNT(0) =2或3时的查询结果里,错在哪儿?
相关推荐
java测试题
java测试题
MYSQL中统计查询结果总行数的便捷方法省去count(*)
MYSQL的关键词 : SQL_CALC_FOUND_ROWS 查看手册后发现此关键词的作用是在查询时统计满足过滤条件后的结果的总数(不受 Limit 的限制) 例如: 代码如下: SELECT SQL_CALC_FOUND_ROWS tid FROM cdb_threads WHERE fid=14 LIMIT 1,10; 假设满足条件的有1000条,这里返回10条。 立即使用 代码如下: SELECT found_rows() AS rowcount; 则返回的 rowcount 为1000; 这样节省了SELECT count(*) AS rowcount的重复查询,可以节省比较
Django ORM判断查询结果是否为空,判断django中的orm为空实例
我就废话不多说了,大家还是直接看代码吧~ result= Booking.objects.filter() #方法一 .exists() if result.exists(): print "QuerySet has Data" else: print "QuerySet is empty" #方法二 .count()==0 if result.count() == 0: print "empty" #方法三 if result: print "QuerySet has Data" else: print "QuerySet is empty" 总结: QuerySet.exist
MySQL中count(*)、count(1)和count(col)的区别汇总
count()函数是用来统计表中记录的一个函数,返回匹配条件的行数,下面这篇文章主要给大家总结介绍了关于MySQL中count(*)、count(1)和count(col)的区别,文中通过示例代码介绍的非常详细,需要的朋友可以参考下。
mysql查询结果输出到文件的方法
mysql查询结果导出/输出/写入到文件 方法一: 直接执行命令: mysql> select count(1) from table into outfile ‘/tmp/test.xls’; Query OK, 31 rows affected (0.00 sec) 在目录/tmp/下会产生文件test.xls 遇到的问题: mysql> select count(1) from table into outfile ‘/data/test.xls’; 报错: ERROR 1 (HY000): Can’t create/write to file ‘/data/test.xls’ (


疑难问题
