怎样查找连续纪录的重复

hansonboy 2017-12-09 12:46:31
是多条顺序纪录相同, 不是单条纪录重复,如:
a表字段
id,num
1 2
2 3
3 4
4 2
5 3
6 4
7 5
8 2
9 3
10 4

我要查找num字段里有几次连续开2,3,4 ,注意,2,3,4是连续纪录,不知明不明白我表达的意思
...全文
189 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
hansonboy 2017-12-09
  • 打赏
  • 举报
回复
谢谢,这条SQL太强大了
中国风 2017-12-09
  • 打赏
  • 举报
回复
查看方法--2.显示记录 e.g.


if not object_id(N'Tempdb..#a') is null
    drop table #a
Go
Create table #a([id] int,[num] int)
Insert #a
select 1,2 union all
select 2,3 union all
select 3,4 union all
select 4,2 union all
select 5,3 union all
select 6,4 union all
select 7,5 union all
select 8,2 union all
select 9,3 union all
select 10,3 union all
select 11,3 union all
select 12,3 union all
select 13,4
GO
DECLARE @str VARCHAR(1000),@Rows INT;

SET @str='2,3,4'	--连续数条件

SET @str='select '+REPLACE(@str,',',' union all select ')
DECLARE @Nr TABLE(ID INT IDENTITY,Num int);
INSERT INTO @Nr
        ( Num )
EXEC(@str)
SET @Rows=@@ROWCOUNT

--1.显示总记录数
SELECT
	COUNT(*) AS 记录数
FROM 
(
SELECT (a.ID-b.id) AS GroupRows
FROM @Nr AS a
	INNER JOIN #a AS b ON b.num=a.Num
GROUP BY a.ID-b.id
HAVING COUNT(*)=@Rows
) AS t
/*
记录数
2
*/
--2.显示记录
;WITH CTET
AS
(
SELECT (a.ID-b.id) AS Grp,b.*
FROM @Nr AS a
	INNER JOIN #a AS b ON b.num=a.Num
)
SELECT  a.ID,a.Num
FROM    CTET AS a
        INNER JOIN ( SELECT Grp
                     FROM   CTET
                     GROUP BY Grp
                     HAVING COUNT(*) = @Rows
                   ) AS b ON a.Grp = b.Grp;
 /*
ID	Num
1	2
2	3
3	4
4	2
5	3
6	4
*/

hansonboy 2017-12-09
  • 打赏
  • 举报
回复
引用 11 楼 roy_88 的回复:
DECLARE @str VARCHAR(1000)='2,3,4'    --连续数条件
,@Rows INT=0;
这一段改改,SQL05不支持声明+赋默认值 e.g.


if not object_id(N'Tempdb..#a') is null
    drop table #a
Go
Create table #a([id] int,[num] int)
Insert #a
select 1,2 union all
select 2,3 union all
select 3,4 union all
select 4,2 union all
select 5,3 union all
select 6,4 union all
select 7,5 union all
select 8,2 union all
select 9,3 union all
select 10,3 union all
select 11,3 union all
select 12,3 union all
select 13,4
GO
DECLARE @str VARCHAR(1000),@Rows INT;

SET @str='2,3,4'	--连续数条件

SET @str='select '+REPLACE(@str,',',' union all select ')
DECLARE @Nr TABLE(ID INT IDENTITY,Num int);
INSERT INTO @Nr
        ( Num )
EXEC(@str)
SET @Rows=@@ROWCOUNT

SELECT
	COUNT(*) AS 记录数
FROM 
(
SELECT (a.ID-b.id) AS GroupRows
FROM @Nr AS a
	INNER JOIN #a AS b ON b.num=a.Num
GROUP BY a.ID-b.id
HAVING COUNT(*)=@Rows
) AS t

/*
记录数
2
*/
你的是对的, 有没有办法可以把纪录显示出来, 不是只显示总数呢?两种结果我都要
二月十六 2017-12-09
  • 打赏
  • 举报
回复
如果传过的值是2,3,4这样的,加一个处理函数:

CREATE FUNCTION dbo.F_Split
(
@SplitString nvarchar(max), --源字符串
@Separator nvarchar(10)=' ' --分隔符号,默认为空格
)
RETURNS @SplitStringsTable TABLE --输出的数据表
(
[id] int identity(1,1),
[value] nvarchar(max)
)
AS
BEGIN
DECLARE @CurrentIndex int;
DECLARE @NextIndex int;
DECLARE @ReturnText nvarchar(max);

SELECT @CurrentIndex=1;
WHILE(@CurrentIndex<=len(@SplitString))
BEGIN
SELECT @NextIndex=charindex(@Separator,@SplitString,@CurrentIndex);
IF(@NextIndex=0 OR @NextIndex IS NULL)
SELECT @NextIndex=len(@SplitString)+1;
SELECT @ReturnText=substring(@SplitString,@CurrentIndex,@NextIndex-@CurrentIndex);
INSERT INTO @SplitStringsTable([value]) VALUES(@ReturnText);
SELECT @CurrentIndex=@NextIndex+1;
END
RETURN;
END
GO

if not object_id(N'Tempdb..#a') is null
drop table #a
Go
Create table #a([id] int,[num] int)
Insert #a
select 1,2 union all
select 2,3 union all
select 3,4 union all
select 4,2 union all
select 5,3 union all
select 6,4 union all
select 7,5 union all
select 8,2 union all
select 9,3 union all
select 10,3 union all
select 11,3 union all
select 12,3 union all
select 13,4
Go
--测试数据结束
DECLARE @str NVARCHAR(100)='2,3,4' --传过来的查询条件
SELECT COUNT(1) FROM (
SELECT COUNT(1) AS rncount ,
#a.id - t.id AS rn
FROM #a
JOIN dbo.F_Split(@str, ',') t ON t.value = #a.num
GROUP BY #a.id - t.id
HAVING COUNT(1) = LEN(REPLACE(@str,',','')))tt


中国风 2017-12-09
  • 打赏
  • 举报
回复
DECLARE @str VARCHAR(1000)='2,3,4'    --连续数条件
,@Rows INT=0;
这一段改改,SQL05不支持声明+赋默认值 e.g.


if not object_id(N'Tempdb..#a') is null
    drop table #a
Go
Create table #a([id] int,[num] int)
Insert #a
select 1,2 union all
select 2,3 union all
select 3,4 union all
select 4,2 union all
select 5,3 union all
select 6,4 union all
select 7,5 union all
select 8,2 union all
select 9,3 union all
select 10,3 union all
select 11,3 union all
select 12,3 union all
select 13,4
GO
DECLARE @str VARCHAR(1000),@Rows INT;

SET @str='2,3,4'	--连续数条件

SET @str='select '+REPLACE(@str,',',' union all select ')
DECLARE @Nr TABLE(ID INT IDENTITY,Num int);
INSERT INTO @Nr
        ( Num )
EXEC(@str)
SET @Rows=@@ROWCOUNT

SELECT
	COUNT(*) AS 记录数
FROM 
(
SELECT (a.ID-b.id) AS GroupRows
FROM @Nr AS a
	INNER JOIN #a AS b ON b.num=a.Num
GROUP BY a.ID-b.id
HAVING COUNT(*)=@Rows
) AS t

/*
记录数
2
*/
hansonboy 2017-12-09
  • 打赏
  • 举报
回复
错误消息: (13 行受影响) 消息 139,级别 15,状态 1,第 0 行 不能向局部变量赋予默认值。 消息 137,级别 15,状态 2,第 3 行 必须声明标量变量 "@str"。 消息 137,级别 15,状态 2,第 7 行 必须声明标量变量 "@str"。 消息 137,级别 15,状态 1,第 8 行 必须声明标量变量 "@Rows"。 消息 137,级别 15,状态 2,第 18 行 必须声明标量变量 "@Rows"。
hansonboy 2017-12-09
  • 打赏
  • 举报
回复
我把代码复制出来执行出错的,我的是SQL2005是不是版本问题
中国风 2017-12-09
  • 打赏
  • 举报
回复
这样效果? e.g.


if not object_id(N'Tempdb..#a') is null
    drop table #a
Go
Create table #a([id] int,[num] int)
Insert #a
select 1,2 union all
select 2,3 union all
select 3,4 union all
select 4,2 union all
select 5,3 union all
select 6,4 union all
select 7,5 union all
select 8,2 union all
select 9,3 union all
select 10,3 union all
select 11,3 union all
select 12,3 union all
select 13,4
GO
DECLARE @str VARCHAR(1000)='2,3,4'	--连续数条件
,@Rows INT=0;

SET @str='select '+REPLACE(@str,',',' union all select ')
DECLARE @Nr TABLE(ID INT IDENTITY,Num int);
INSERT INTO @Nr
        ( Num )
EXEC(@str)
SET @Rows=@@ROWCOUNT

SELECT
	COUNT(*) AS 记录数
FROM 
(
SELECT (a.ID-b.id) AS GroupRows
FROM @Nr AS a
	INNER JOIN #a AS b ON b.num=a.Num
GROUP BY a.ID-b.id
HAVING COUNT(*)=@Rows
) AS t

/*
记录数
2
*/
hansonboy 2017-12-09
  • 打赏
  • 举报
回复
引用 6 楼 sinat_28984567 的回复:
是这个意思?
--测试数据
if not object_id(N'Tempdb..#a') is null
	drop table #a
Go
Create table #a([id] int,[num] int)
Insert #a
select 1,2 union all
select 2,3 union all
select 3,4 union all
select 4,2 union all
select 5,3 union all
select 6,4 union all
select 7,5 union all
select 8,2 union all
select 9,3 union all
select 10,4
Go
--测试数据结束
SELECT  COUNT(DISTINCT id - num)
FROM    #a 
[quote=引用 5 楼 hansonboy 的回复:] [quote=引用 4 楼 sinat_28984567 的回复:] 上边的例子2,3,4是查询条件?(这个条件可变)
是,这个条件可变 1楼回答的如果我多加几条数据进去就不行了,如 if not object_id(N'Tempdb..#a') is null drop table #a Go Create table #a([id] int,[num] int) Insert #a select 1,2 union all select 2,3 union all select 3,4 union all select 4,2 union all select 5,3 union all select 6,4 union all select 7,5 union all select 8,2 union all select 9,3 union all select 10,3 union all select 11,3 union all select 12,3 union all select 13,4 Go --测试数据结束 SELECT COUNT(DISTINCT id - num) FROM #a 还是查找2,3,4结果是6的,应该是4才对因为我加的几条不是2,3,4[/quote] 结果为啥是4呢?是求2、3、4的连续次数吗?应该是2啊[/quote] 对,是2才对
二月十六 2017-12-09
  • 打赏
  • 举报
回复
是这个意思?
--测试数据
if not object_id(N'Tempdb..#a') is null
	drop table #a
Go
Create table #a([id] int,[num] int)
Insert #a
select 1,2 union all
select 2,3 union all
select 3,4 union all
select 4,2 union all
select 5,3 union all
select 6,4 union all
select 7,5 union all
select 8,2 union all
select 9,3 union all
select 10,4
Go
--测试数据结束
SELECT  COUNT(DISTINCT id - num)
FROM    #a 
引用 5 楼 hansonboy 的回复:
[quote=引用 4 楼 sinat_28984567 的回复:] 上边的例子2,3,4是查询条件?(这个条件可变)
是,这个条件可变 1楼回答的如果我多加几条数据进去就不行了,如 if not object_id(N'Tempdb..#a') is null drop table #a Go Create table #a([id] int,[num] int) Insert #a select 1,2 union all select 2,3 union all select 3,4 union all select 4,2 union all select 5,3 union all select 6,4 union all select 7,5 union all select 8,2 union all select 9,3 union all select 10,3 union all select 11,3 union all select 12,3 union all select 13,4 Go --测试数据结束 SELECT COUNT(DISTINCT id - num) FROM #a 还是查找2,3,4结果是6的,应该是4才对因为我加的几条不是2,3,4[/quote] 结果为啥是4呢?是求2、3、4的连续次数吗?应该是2啊
hansonboy 2017-12-09
  • 打赏
  • 举报
回复
引用 4 楼 sinat_28984567 的回复:
上边的例子2,3,4是查询条件?(这个条件可变)
是,这个条件可变 1楼回答的如果我多加几条数据进去就不行了,如 if not object_id(N'Tempdb..#a') is null drop table #a Go Create table #a([id] int,[num] int) Insert #a select 1,2 union all select 2,3 union all select 3,4 union all select 4,2 union all select 5,3 union all select 6,4 union all select 7,5 union all select 8,2 union all select 9,3 union all select 10,3 union all select 11,3 union all select 12,3 union all select 13,4 Go --测试数据结束 SELECT COUNT(DISTINCT id - num) FROM #a 还是查找2,3,4结果是6的,应该是4才对因为我加的几条不是2,3,4
二月十六 2017-12-09
  • 打赏
  • 举报
回复
上边的例子2,3,4是查询条件?(这个条件可变)
二月十六 2017-12-09
  • 打赏
  • 举报
回复
引用 2楼我是你的主体 的回复:
可能是我表达不够清楚, 我上面的数据只是一个例子, 我要找的数据是自定义的,可能是找2,3,4(3个),也有可能是找3,5,6,7(4个且不是连数),要找出按ID排序有没有连续出现给出的数
不用看测试数据,测试数据是按你给的数据举的例子,换成实际表数据就行了,楼主最好给出最后想要的结果。
hansonboy 2017-12-09
  • 打赏
  • 举报
回复
可能是我表达不够清楚, 我上面的数据只是一个例子, 我要找的数据是自定义的,可能是找2,3,4(3个),也有可能是找3,5,6,7(4个且不是连数),要找出按ID排序有没有连续出现给出的数
二月十六 2017-12-09
  • 打赏
  • 举报
回复
最后的结果是什么?是这个意思?
--测试数据
if not object_id(N'Tempdb..#a') is null
drop table #a
Go
Create table #a([id] int,[num] int)
Insert #a
select 1,2 union all
select 2,3 union all
select 3,4 union all
select 4,2 union all
select 5,3 union all
select 6,4 union all
select 7,5 union all
select 8,2 union all
select 9,3 union all
select 10,4
Go
--测试数据结束
SELECT COUNT(DISTINCT id - num)
FROM #a


22,301

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧