22,210
社区成员
发帖
与我相关
我的任务
分享
--测试用
if object_id('tempdb.dbo.#tb1') is not null drop table #tb1
if object_id('tempdb.dbo.#tb2') is not null drop table #tb2
go
CREATE TABLE #tb1(
[NoText] [nvarchar](20) NULL)
insert #tb1
select '01 21 23 25 31 33' union all ---第1行记录,6个号码01 21 23 25 31 33
select '02 21 23 25 31 33' ---第2行记录,也是6个号码
CREATE TABLE #tb2(
[NoText] [nvarchar](20) NULL)
insert #tb2
select '01 03 21 23 25 31' union all
select '02 06 21 23 25 31' union all
select '03 08 21 23 25 31' union all
select '04 13 21 23 25 31' union all
select '05 15 21 23 25 31' union all
select '06 21 23 24 25 31' union all
select '07 21 23 25 29 31' union all
select '08 21 23 25 30 31' union all
select '09 21 23 25 31 32' union all
select '10 21 23 25 31 33' union all
select '01 03 21 23 25 33' union all
select '01 06 21 23 25 33' union all
select '01 08 21 23 25 33' union all
select '01 13 21 23 25 33' union all
select '01 15 21 23 25 33' union all
select '01 21 23 24 25 33' union all
select '02 21 23 25 29 33' union all
select '03 21 23 25 30 33' union all
select '04 21 23 25 31 33' union all
select '05 21 23 25 32 33' union all
select '01 03 23 25 31 33' union all
select '01 06 23 25 31 33' union all
select '01 08 23 25 31 33' union all
select '01 13 23 25 31 33' union all
select '01 15 23 25 31 33' union all
select '11 21 23 25 31 33' union all
select '01 23 24 25 31 33' union all
select '02 23 25 29 31 33' union all
select '03 23 25 30 31 33' union all
select '04 23 25 31 32 33' union all
select '01 03 21 25 31 33' union all
select '01 06 21 25 31 33' union all
select '01 08 21 25 31 33' union all
select '01 13 21 25 31 33' union all
select '01 15 21 25 31 33' union all
select '01 21 23 25 31 33' union all
select '02 21 24 25 31 33' union all
select '03 21 25 29 31 33' union all
select '04 21 25 30 31 33' union all
select '05 21 25 31 32 33' union all
select '01 03 21 23 31 33' union all
select '01 06 21 23 31 33' union all
select '01 08 21 23 31 33' union all
select '01 13 21 23 31 33' union all
select '01 15 21 23 31 33' union all
select '01 21 23 24 31 33' union all
select '20 21 23 25 31 33' union all
select '01 21 23 29 31 33' union all
select '02 21 23 30 31 33' union all
select '03 21 23 31 32 33' union all
select '04 21 23 25 31 33' union all
select '03 21 23 25 31 33' union all
select '06 21 23 25 31 33' union all
select '08 21 23 25 31 33' union all
select '13 21 23 25 31 33' union all
select '15 21 23 25 31 33' union all
select '21 23 24 25 31 33' union all
select '11 23 25 29 31 33' union all
select '12 23 25 30 31 33' union all
select '13 23 25 31 32 33'
---需要的结果 基础数据 #tb1 与 数据表#tb2 对比分析,找出5个号码 是相同的记录
SELECT NoText FROM #tb2
where notext like '%01%'
and notext like '%21%'
and notext like '%23%'
and notext like '%25%'
and notext like '%31%'
union all
SELECT NoText FROM tb17500cn
where notext like '%01%'
and notext like '%21%'
and notext like '%23%'
and notext like '%25%'
and notext like '%33%'
union all
SELECT NoText FROM tb17500cn
where notext like '%01%'
and notext like '%31%'
and notext like '%23%'
and notext like '%25%'
and notext like '%33%'
union all
SELECT NoText FROM tb17500cn
where notext like '%01%'
and notext like '%21%'
and notext like '%25%'
and notext like '%31%'
and notext like '%33%'
union all
SELECT NoText FROM tb17500cn
where notext like '%01%'
and notext like '%21%'
and notext like '%23%'
and notext like '%31%'
and notext like '%33%'
union all
SELECT NoText FROM tb17500cn
where notext like '%31%'
and notext like '%21%'
and notext like '%23%'
and notext like '%25%'
and notext like '%33%'
--问题:以上代码是 #tb1 第1行记录的6个号码01 21 23 25 31 33与 #tb2 对比分析,找出了5个号码 是相同的记录,
--但 #tb1 还有第2、3、4、5......N行记录,如何用代码一次性完成?
DECLARE @notext VARCHAR(50)
DECLARE my_cur CURSOR FOR
SELECT top 100 notext FROM #tb1
OPEN my_cur
FETCH NEXT FROM my_cur INTO @notext
IF NOT OBJECT_ID('#tab3') IS NOT NULL
BEGIN
DROP TABLE #tab3
CREATE TABLE #tab3
(
notext VARCHAR(20)
)
END
WHILE @@FETCH_STATUS=0
BEGIN
with cte1 as
(select a.NoText,' '+a.NoText+' ' as NoText_2,' '+b.NoText+' ' as NoText_1
from #tb2 a
INNER JOIN (SELECT @notext AS notext) b on 1=1
)
INSERT INTO #tab3
select distinct NoText
from
(select *,
(case when charindex(substring(NoText_1,1,4),NoText_2)>0 then 1 else 0 end+
case when charindex(substring(NoText_1,4,4),NoText_2)>0 then 1 else 0 end+
case when charindex(substring(NoText_1,7,4),NoText_2)>0 then 1 else 0 end+
case when charindex(substring(NoText_1,10,4),NoText_2)>0 then 1 else 0 end+
case when charindex(substring(NoText_1,13,4),NoText_2)>0 then 1 else 0 end+
case when charindex(substring(NoText_1,16,4),NoText_2)>0 then 1 else 0 end) as match
from cte1) as a
where match>=5
FETCH NEXT FROM my_cur INTO @notext
END
CLOSE my_cur
DEALLOCATE my_cur
SELECT DISTINCT notext FROM #tab3
类似这样,上面写了top 100,你可以先跑100条看看,看是否跑的动,如果跑的动就把top 100去掉
with cte1
as
(select distinct a.NoText,a.NoText as NoText_2,b.NoText as NoText_1 from #tb2 a
join #tb1 b on 1=1)
selet distinct NoText
from
(select *,
(case when charindex(substring(NoText_1,1,2),NoText_2)>0 then 1 else 0 end+
case when charindex(substring(NoText_1,4,2),NoText_2)>0 then 1 else 0 end+
case when charindex(substring(NoText_1,7,2),NoText_2)>0 then 1 else 0 end+
case when charindex(substring(NoText_1,10,2),NoText_2)>0 then 1 else 0 end+
case when charindex(substring(NoText_1,13,2),NoText_2)>0 then 1 else 0 end+
case when charindex(substring(NoText_1,16,2),NoText_2)>0 then 1 else 0 end) as match
from cte1) as a
where match>=5
谢谢12 楼 qq_37170555 的回复,楼主对于游标的使用很是菜鸟,请多指导,谢谢啊。
with cte1
as
(select a.NoText,' '+a.NoText+' ' as NoText_2,' '+b.NoText+' ' as NoText_1 from #tb2 a
join #tb1 b on 1=1)
select distinct NoText
from
(select *,
(case when charindex(substring(NoText_1,1,4),NoText_2)>0 then 1 else 0 end+
case when charindex(substring(NoText_1,4,4),NoText_2)>0 then 1 else 0 end+
case when charindex(substring(NoText_1,7,4),NoText_2)>0 then 1 else 0 end+
case when charindex(substring(NoText_1,10,4),NoText_2)>0 then 1 else 0 end+
case when charindex(substring(NoText_1,13,4),NoText_2)>0 then 1 else 0 end+
case when charindex(substring(NoText_1,16,4),NoText_2)>0 then 1 else 0 end) as match
from cte1) as a
where match>=5
你想想1200 inner join 120万的数据是多少啊,十几个亿啊。然后你下面在用这些字符串函数,再加上distinct 。跑九个半小时不出来也是正常的。我的建议你把#tb1的数据分开n多次和#tb2连一起,或者你用游标1条1条的去把#tb1的数据和#tb2数据连一起。这样一次只出来120万数据,应该也不需要多久可以跑出来的
with cte1
as
(select distinct a.NoText,a.NoText as NoText_2,b.NoText as NoText_1 from #tb2 a
join #tb1 b on 1=1)
selet distinct NoText
from
(select *,
(case when charindex(substring(NoText_1,1,2),NoText_2)>0 then 1 else 0 end+
case when charindex(substring(NoText_1,4,2),NoText_2)>0 then 1 else 0 end+
case when charindex(substring(NoText_1,7,2),NoText_2)>0 then 1 else 0 end+
case when charindex(substring(NoText_1,10,2),NoText_2)>0 then 1 else 0 end+
case when charindex(substring(NoText_1,13,2),NoText_2)>0 then 1 else 0 end+
case when charindex(substring(NoText_1,16,2),NoText_2)>0 then 1 else 0 end) as match
from cte1) as a
where match>=5
这是10 楼 RINK_1 的代码经过优化后
TOP1 耗时 10秒 结果 (163 行受影响)
TOP10 耗时 98秒 结果 (1630 行受影响)
TOP100 耗时 16‘’01秒 结果 (16300 行受影响)
问题:面对22亿行的庞大数据量,一定学习游标的代码,游标的优势在哪里?两者代码对比后,效率没有体现出来啊。
补充:
【楼主的环境】固盘,WIN7,Microsoft SQL Server 2014
Microsoft SQL Server Management Studio 12.0.2269.0
Microsoft Analysis Services 客户端工具 12.0.2000.8
Microsoft 数据访问组件 (MDAC) 6.1.7601.17514
Microsoft MSXML 3.0 6.0
Microsoft Internet Explorer 8.0.7601.17514
Microsoft .NET Framework 4.0.30319.1026
操作系统 6.1.7601[/quote]
这里之所以使用游标,是因为你的#tb2的数据量太大了,用游标一行一行去读取#tb1的数据,然后再和#tb2连起来,这样的话就不会因为#tb1的数据太多,导致表相连的时候查询不来数据。一般情况下最好不要用游标,能不用的时候尽量不要用,因为在性能上,游标会吃更多的内存,减少可用的并发,占用宽带,锁定资源,当然还有更多的代码量……
对于SQL的优化我也还在学习中,你可以请教一下论然的大神,他们很厉害。我能帮你的就只是写个sql代码出来结果。对于大数据优化我还在摸索中,爱莫能助了兄弟
with cte1
as
(select distinct a.NoText,a.NoText as NoText_2,b.NoText as NoText_1 from #tb2 a
join #tb1 b on 1=1)
selet distinct NoText
from
(select *,
(case when charindex(substring(NoText_1,1,2),NoText_2)>0 then 1 else 0 end+
case when charindex(substring(NoText_1,4,2),NoText_2)>0 then 1 else 0 end+
case when charindex(substring(NoText_1,7,2),NoText_2)>0 then 1 else 0 end+
case when charindex(substring(NoText_1,10,2),NoText_2)>0 then 1 else 0 end+
case when charindex(substring(NoText_1,13,2),NoText_2)>0 then 1 else 0 end+
case when charindex(substring(NoText_1,16,2),NoText_2)>0 then 1 else 0 end) as match
from cte1) as a
where match>=5
这是10 楼 RINK_1 的代码经过优化后
TOP1 耗时 10秒 结果 (163 行受影响)
TOP10 耗时 98秒 结果 (1630 行受影响)
TOP100 耗时 16‘’01秒 结果 (16300 行受影响)
问题:面对22亿行的庞大数据量,一定学习游标的代码,游标的优势在哪里?两者代码对比后,效率没有体现出来啊。
补充:
【楼主的环境】固盘,WIN7,Microsoft SQL Server 2014
Microsoft SQL Server Management Studio 12.0.2269.0
Microsoft Analysis Services 客户端工具 12.0.2000.8
Microsoft 数据访问组件 (MDAC) 6.1.7601.17514
Microsoft MSXML 3.0 6.0
Microsoft Internet Explorer 8.0.7601.17514
Microsoft .NET Framework 4.0.30319.1026
操作系统 6.1.7601
btxt
--------------------
01 03 21 23 25 31
01 03 21 23 25 33
01 03 21 23 31 33
01 03 21 25 31 33
01 03 23 25 31 33
01 06 21 23 25 33
01 06 21 23 31 33
01 06 21 25 31 33
01 06 23 25 31 33
01 08 21 23 25 33
01 08 21 23 31 33
01 08 21 25 31 33
01 08 23 25 31 33
01 13 21 23 25 33
01 13 21 23 31 33
01 13 21 25 31 33
01 13 23 25 31 33
01 15 21 23 25 33
01 15 21 23 31 33
01 15 21 25 31 33
01 15 23 25 31 33
01 21 23 24 25 33
01 21 23 24 31 33
01 21 23 25 31 33
01 21 23 29 31 33
01 23 24 25 31 33
02 06 21 23 25 31
02 21 23 25 29 33
02 21 23 30 31 33
02 21 24 25 31 33
02 23 25 29 31 33
03 21 23 25 31 33
04 21 23 25 31 33
06 21 23 25 31 33
08 21 23 25 31 33
10 21 23 25 31 33
11 21 23 25 31 33
13 21 23 25 31 33
15 21 23 25 31 33
20 21 23 25 31 33
21 23 24 25 31 33
(41 行受影响)
--如果不要左右对照的话那么出来的结果就是这个了
atxt btxt
-------------------- --------------------
01 21 23 25 31 33 01 03 21 23 25 31
01 21 23 25 31 33 01 03 21 23 25 33
01 21 23 25 31 33 01 03 21 23 31 33
01 21 23 25 31 33 01 03 21 25 31 33
01 21 23 25 31 33 01 03 23 25 31 33
01 21 23 25 31 33 01 06 21 23 25 33
01 21 23 25 31 33 01 06 21 23 31 33
01 21 23 25 31 33 01 06 21 25 31 33
01 21 23 25 31 33 01 06 23 25 31 33
01 21 23 25 31 33 01 08 21 23 25 33
01 21 23 25 31 33 01 08 21 23 31 33
01 21 23 25 31 33 01 08 21 25 31 33
01 21 23 25 31 33 01 08 23 25 31 33
01 21 23 25 31 33 01 13 21 23 25 33
01 21 23 25 31 33 01 13 21 23 31 33
01 21 23 25 31 33 01 13 21 25 31 33
01 21 23 25 31 33 01 13 23 25 31 33
01 21 23 25 31 33 01 15 21 23 25 33
01 21 23 25 31 33 01 15 21 23 31 33
01 21 23 25 31 33 01 15 21 25 31 33
01 21 23 25 31 33 01 15 23 25 31 33
01 21 23 25 31 33 01 21 23 24 25 33
01 21 23 25 31 33 01 21 23 24 31 33
01 21 23 25 31 33 01 21 23 29 31 33
01 21 23 25 31 33 01 23 24 25 31 33
01 21 23 25 31 33 03 21 23 25 31 33
01 21 23 25 31 33 04 21 23 25 31 33
01 21 23 25 31 33 06 21 23 25 31 33
01 21 23 25 31 33 08 21 23 25 31 33
01 21 23 25 31 33 10 21 23 25 31 33
01 21 23 25 31 33 11 21 23 25 31 33
01 21 23 25 31 33 13 21 23 25 31 33
01 21 23 25 31 33 15 21 23 25 31 33
01 21 23 25 31 33 20 21 23 25 31 33
01 21 23 25 31 33 21 23 24 25 31 33
02 21 23 25 31 33 01 21 23 25 31 33
02 21 23 25 31 33 02 06 21 23 25 31
02 21 23 25 31 33 02 21 23 25 29 33
02 21 23 25 31 33 02 21 23 30 31 33
02 21 23 25 31 33 02 21 24 25 31 33
02 21 23 25 31 33 02 23 25 29 31 33
02 21 23 25 31 33 03 21 23 25 31 33
02 21 23 25 31 33 04 21 23 25 31 33
02 21 23 25 31 33 06 21 23 25 31 33
02 21 23 25 31 33 08 21 23 25 31 33
02 21 23 25 31 33 10 21 23 25 31 33
02 21 23 25 31 33 11 21 23 25 31 33
02 21 23 25 31 33 13 21 23 25 31 33
02 21 23 25 31 33 15 21 23 25 31 33
02 21 23 25 31 33 20 21 23 25 31 33
02 21 23 25 31 33 21 23 24 25 31 33
(51 行受影响)
--测试用
if object_id('tempdb.dbo.#tb1') is not null drop table #tb1
if object_id('tempdb.dbo.#tb2') is not null drop table #tb2
go
CREATE TABLE #tb1(
[NoText] [nvarchar](20) NULL)
insert #tb1
select '01 21 23 25 31 33' union all ---第1行记录,6个号码01 21 23 25 31 33
select '02 21 23 25 31 33' ---第2行记录,也是6个号码
CREATE TABLE #tb2(
[NoText] [nvarchar](20) NULL)
insert #tb2
select '01 03 21 23 25 31' union all
select '02 06 21 23 25 31' union all
select '03 08 21 23 25 31' union all
select '04 13 21 23 25 31' union all
select '05 15 21 23 25 31' union all
select '06 21 23 24 25 31' union all
select '07 21 23 25 29 31' union all
select '08 21 23 25 30 31' union all
select '09 21 23 25 31 32' union all
select '10 21 23 25 31 33' union all
select '01 03 21 23 25 33' union all
select '01 06 21 23 25 33' union all
select '01 08 21 23 25 33' union all
select '01 13 21 23 25 33' union all
select '01 15 21 23 25 33' union all
select '01 21 23 24 25 33' union all
select '02 21 23 25 29 33' union all
select '03 21 23 25 30 33' union all
select '04 21 23 25 31 33' union all
select '05 21 23 25 32 33' union all
select '01 03 23 25 31 33' union all
select '01 06 23 25 31 33' union all
select '01 08 23 25 31 33' union all
select '01 13 23 25 31 33' union all
select '01 15 23 25 31 33' union all
select '11 21 23 25 31 33' union all
select '01 23 24 25 31 33' union all
select '02 23 25 29 31 33' union all
select '03 23 25 30 31 33' union all
select '04 23 25 31 32 33' union all
select '01 03 21 25 31 33' union all
select '01 06 21 25 31 33' union all
select '01 08 21 25 31 33' union all
select '01 13 21 25 31 33' union all
select '01 15 21 25 31 33' union all
select '01 21 23 25 31 33' union all
select '02 21 24 25 31 33' union all
select '03 21 25 29 31 33' union all
select '04 21 25 30 31 33' union all
select '05 21 25 31 32 33' union all
select '01 03 21 23 31 33' union all
select '01 06 21 23 31 33' union all
select '01 08 21 23 31 33' union all
select '01 13 21 23 31 33' union all
select '01 15 21 23 31 33' union all
select '01 21 23 24 31 33' union all
select '20 21 23 25 31 33' union all
select '01 21 23 29 31 33' union all
select '02 21 23 30 31 33' union all
select '03 21 23 31 32 33' union all
select '04 21 23 25 31 33' union all
select '03 21 23 25 31 33' union all
select '06 21 23 25 31 33' union all
select '08 21 23 25 31 33' union all
select '13 21 23 25 31 33' union all
select '15 21 23 25 31 33' union all
select '21 23 24 25 31 33' union all
select '11 23 25 29 31 33' union all
select '12 23 25 30 31 33' union all
select '13 23 25 31 32 33'
--测试数据结束
WITH cte AS (
SELECT REPLACE([NOTEXT],' ','') AS [notext] , row_number() OVER ( ORDER BY [NOTEXT] ) as num FROM #tb1
)
,cte3 AS (
SELECT substring(notext,(number*2)+1,2) AS Txtnum,num
FROM MASTER..spt_values,cte
WHERE number<6 AND TYPE='p'
)
, cte2 AS (
SELECT REPLACE([NOTEXT],' ','') AS [notext] , row_number() OVER ( ORDER BY [NOTEXT] ) as num FROM #tb2
)
,cte4 AS (
SELECT substring(notext,(number*2)+1,2) AS Txtnum,num
FROM MASTER..spt_values,cte2
WHERE number<6 AND TYPE='p'
)
,cte5 AS (
SELECT a.txtnum AS atxtnum,a.num AS anum,b.txtnum AS btxtnum,b.num AS bnum,COUNT(b.num) OVER(PARTITION BY b.num,a.num) AS cnt FROM cte3 a
inner JOIN cte4 b ON a.txtnum=b.txtnum --AND b.num>=a.num
)
--但愿你可以理解以上的逻辑,不理解也没事,反正这样能出来,而且绝对没问题
SELECT distinct b.[notext] AS atxt, c.[NOTEXT] AS btxt
FROM cte5 a
left OUTER JOIN cte b ON a.anum=b.num
left OUTER JOIN cte2 c ON a.bnum=c.num
where a.cnt=5
ORDER BY b.[notext]
原本出来数据应该是53条的,但是由于你的#tb2有一条数据是重复的,所以出来的数据为51条CREATE FUNCTION dbo.f_splitstr(@SourceSql NVARCHAR(MAX),@StrSeprate VARCHAR(100))
RETURNS @temp TABLE(F1 VARCHAR(100))
AS
BEGIN
DECLARE @ch AS VARCHAR(100)
SET @SourceSql=@SourceSql+@StrSeprate
WHILE(@SourceSql<>'')
BEGIN
SET @ch=LEFT(@SourceSql,CHARINDEX(@StrSeprate,@SourceSql,1)-1)
INSERT @temp VALUES(@ch)
SET @SourceSql=STUFF(@SourceSql,1,CHARINDEX(@StrSeprate,@SourceSql,1),'')
END
RETURN
END
GO
--测试用
if object_id('tempdb.dbo.#tb1') is not null drop table #tb1
if object_id('tempdb.dbo.#tb2') is not null drop table #tb2
go
CREATE TABLE #tb1(
[NoText] [nvarchar](20) NULL)
insert #tb1
select '01 21 23 25 31 33' union all ---第1行记录,6个号码01 21 23 25 31 33
select '02 21 23 25 31 33' ---第2行记录,也是6个号码
CREATE TABLE #tb2(
[NoText] [nvarchar](20) NULL)
insert #tb2
select '01 03 21 23 25 31' union all
select '02 06 21 23 25 31' union all
select '03 08 21 23 25 31' union all
select '04 13 21 23 25 31' union all
select '05 15 21 23 25 31' union all
select '06 21 23 24 25 31' union all
select '07 21 23 25 29 31' union all
select '08 21 23 25 30 31' union all
select '09 21 23 25 31 32' union all
select '10 21 23 25 31 33' union all
select '01 03 21 23 25 33' union all
select '01 06 21 23 25 33' union all
select '01 08 21 23 25 33' union all
select '01 13 21 23 25 33' union all
select '01 15 21 23 25 33' union all
select '01 21 23 24 25 33' union all
select '02 21 23 25 29 33' union all
select '03 21 23 25 30 33' union all
select '04 21 23 25 31 33' union all
select '05 21 23 25 32 33' union all
select '01 03 23 25 31 33' union all
select '01 06 23 25 31 33' union all
select '01 08 23 25 31 33' union all
select '01 13 23 25 31 33' union all
select '01 15 23 25 31 33' union all
select '11 21 23 25 31 33' union all
select '01 23 24 25 31 33' union all
select '02 23 25 29 31 33' union all
select '03 23 25 30 31 33' union all
select '04 23 25 31 32 33' union all
select '01 03 21 25 31 33' union all
select '01 06 21 25 31 33' union all
select '01 08 21 25 31 33' union all
select '01 13 21 25 31 33' union all
select '01 15 21 25 31 33' union all
select '01 21 23 25 31 33' union all
select '02 21 24 25 31 33' union all
select '03 21 25 29 31 33' union all
select '04 21 25 30 31 33' union all
select '05 21 25 31 32 33' union all
select '01 03 21 23 31 33' union all
select '01 06 21 23 31 33' union all
select '01 08 21 23 31 33' union all
select '01 13 21 23 31 33' union all
select '01 15 21 23 31 33' union all
select '01 21 23 24 31 33' union all
select '20 21 23 25 31 33' union all
select '01 21 23 29 31 33' union all
select '02 21 23 30 31 33' union all
select '03 21 23 31 32 33' union all
select '04 21 23 25 31 33' union all
select '03 21 23 25 31 33' union all
select '06 21 23 25 31 33' union all
select '08 21 23 25 31 33' union all
select '13 21 23 25 31 33' union all
select '15 21 23 25 31 33' union all
select '21 23 24 25 31 33' union all
select '11 23 25 29 31 33' union all
select '12 23 25 30 31 33' union all
select '13 23 25 31 32 33'
---需要的结果 基础数据 #tb1 与 数据表#tb2 对比分析,找出5个号码 是相同的记录
SELECT #tb2.*
FROM #tb1 ,
#tb2
WHERE ( SELECT COUNT(1)
FROM dbo.f_splitstr(#tb1.NoText, ' ') AS t1
JOIN dbo.f_splitstr(#tb2.NoText, ' ') AS t2 ON t1.F1 = t2.f1
) > 4
notext
01 03 21 23 25 31
01 03 21 23 25 33
01 03 21 23 31 33
01 03 21 25 31 33
01 03 23 25 31 33
01 06 21 23 25 33
01 06 21 23 31 33
01 06 21 25 31 33
01 06 23 25 31 33
01 08 21 23 25 33
01 08 21 23 31 33
01 08 21 25 31 33
01 08 23 25 31 33
01 13 21 23 25 33
01 13 21 23 31 33
01 13 21 25 31 33
01 13 23 25 31 33
01 15 21 23 25 33
01 15 21 23 31 33
01 15 21 25 31 33
01 15 23 25 31 33
01 21 23 24 25 33
01 21 23 24 31 33
01 21 23 25 31 33
01 21 23 29 31 33
01 23 24 25 31 33
02 06 21 23 25 31
02 21 23 25 29 33
02 21 23 30 31 33
02 21 24 25 31 33
02 23 25 29 31 33
03 21 23 25 31 33
04 21 23 25 31 33
06 21 23 25 31 33
08 21 23 25 31 33
10 21 23 25 31 33
11 21 23 25 31 33
13 21 23 25 31 33
15 21 23 25 31 33
20 21 23 25 31 33
21 23 24 25 31 33
NoText
01 03 21 23 25 31
01 21 23 25 31 33
01 03 21 23 25 33
01 06 21 23 25 33
01 08 21 23 25 33
01 13 21 23 25 33
01 15 21 23 25 33
01 21 23 24 25 33
01 21 23 25 31 33
01 03 23 25 31 33
01 06 23 25 31 33
01 08 23 25 31 33
01 13 23 25 31 33
01 15 23 25 31 33
01 23 24 25 31 33
01 21 23 25 31 33
01 03 21 25 31 33
01 06 21 25 31 33
01 08 21 25 31 33
01 13 21 25 31 33
01 15 21 25 31 33
01 21 23 25 31 33
01 21 23 25 31 33
01 03 21 23 31 33
01 06 21 23 31 33
01 08 21 23 31 33
01 13 21 23 31 33
01 15 21 23 31 33
01 21 23 24 31 33
01 21 23 29 31 33
10 21 23 25 31 33
04 21 23 25 31 33
11 21 23 25 31 33
01 21 23 25 31 33
20 21 23 25 31 33
04 21 23 25 31 33
03 21 23 25 31 33
06 21 23 25 31 33
08 21 23 25 31 33
13 21 23 25 31 33
15 21 23 25 31 33
21 23 24 25 31 33
02 06 21 23 25 31
02 21 23 25 29 33
02 23 25 29 31 33
02 21 24 25 31 33
02 21 23 30 31 33
10 21 23 25 31 33
04 21 23 25 31 33
11 21 23 25 31 33
01 21 23 25 31 33
20 21 23 25 31 33
04 21 23 25 31 33
03 21 23 25 31 33
06 21 23 25 31 33
08 21 23 25 31 33
13 21 23 25 31 33
15 21 23 25 31 33
21 23 24 25 31 33
SELECT REPLACE([NOTEXT],' ','') AS [notext]
[/quote]
;
WITH cte AS (
SELECT notext,REPLACE([NOTEXT],' ','') AS [anotext] , row_number() OVER ( ORDER BY [NOTEXT] ) as num FROM #tb1
)
,cte3 AS (
SELECT substring(anotext,(number*2)+1,2) AS Txtnum,num
FROM MASTER..spt_values,cte
WHERE number<6 AND TYPE='p'
)
, cte2 AS (
SELECT notext,REPLACE([NOTEXT],' ','') AS [bnotext] , row_number() OVER ( ORDER BY [NOTEXT] ) as num FROM #tb2
)
,cte4 AS (
SELECT substring(bnotext,(number*2)+1,2) AS Txtnum,num
FROM MASTER..spt_values,cte2
WHERE number<6 AND TYPE='p'
)
,cte5 AS (
SELECT a.txtnum AS atxtnum,a.num AS anum,b.txtnum AS btxtnum,b.num AS bnum,COUNT(b.num) OVER(PARTITION BY b.num,a.num) AS cnt FROM cte3 a
inner JOIN cte4 b ON a.txtnum=b.txtnum --AND b.num>=a.num
)
--SELECT * FROM cte5 where cnt=5
--ORDER BY anum, bnum
--SELECT * FROM cte
SELECT distinct b.[NOTEXT] AS atxt, c.[NOTEXT] AS btxt
FROM cte5 a
left OUTER JOIN cte b ON a.anum=b.num
left OUTER JOIN cte2 c ON a.bnum=c.num
where a.cnt=5
ORDER BY b.[NOTEXT], c.[NOTEXT]
-------------------结果集----------------------------
atxt btxt
-------------------- --------------------
01 21 23 25 31 33 01 03 21 23 25 31
01 21 23 25 31 33 01 03 21 23 25 33
01 21 23 25 31 33 01 03 21 23 31 33
01 21 23 25 31 33 01 03 21 25 31 33
01 21 23 25 31 33 01 03 23 25 31 33
01 21 23 25 31 33 01 06 21 23 25 33
01 21 23 25 31 33 01 06 21 23 31 33
01 21 23 25 31 33 01 06 21 25 31 33
01 21 23 25 31 33 01 06 23 25 31 33
01 21 23 25 31 33 01 08 21 23 25 33
01 21 23 25 31 33 01 08 21 23 31 33
01 21 23 25 31 33 01 08 21 25 31 33
01 21 23 25 31 33 01 08 23 25 31 33
01 21 23 25 31 33 01 13 21 23 25 33
01 21 23 25 31 33 01 13 21 23 31 33
01 21 23 25 31 33 01 13 21 25 31 33
01 21 23 25 31 33 01 13 23 25 31 33
01 21 23 25 31 33 01 15 21 23 25 33
01 21 23 25 31 33 01 15 21 23 31 33
01 21 23 25 31 33 01 15 21 25 31 33
01 21 23 25 31 33 01 15 23 25 31 33
01 21 23 25 31 33 01 21 23 24 25 33
01 21 23 25 31 33 01 21 23 24 31 33
01 21 23 25 31 33 01 21 23 29 31 33
01 21 23 25 31 33 01 23 24 25 31 33
01 21 23 25 31 33 03 21 23 25 31 33
01 21 23 25 31 33 04 21 23 25 31 33
01 21 23 25 31 33 06 21 23 25 31 33
01 21 23 25 31 33 08 21 23 25 31 33
01 21 23 25 31 33 10 21 23 25 31 33
01 21 23 25 31 33 11 21 23 25 31 33
01 21 23 25 31 33 13 21 23 25 31 33
01 21 23 25 31 33 15 21 23 25 31 33
01 21 23 25 31 33 20 21 23 25 31 33
01 21 23 25 31 33 21 23 24 25 31 33
02 21 23 25 31 33 01 21 23 25 31 33
02 21 23 25 31 33 02 06 21 23 25 31
02 21 23 25 31 33 02 21 23 25 29 33
02 21 23 25 31 33 02 21 23 30 31 33
02 21 23 25 31 33 02 21 24 25 31 33
02 21 23 25 31 33 02 23 25 29 31 33
02 21 23 25 31 33 03 21 23 25 31 33
02 21 23 25 31 33 04 21 23 25 31 33
02 21 23 25 31 33 06 21 23 25 31 33
02 21 23 25 31 33 08 21 23 25 31 33
02 21 23 25 31 33 10 21 23 25 31 33
02 21 23 25 31 33 11 21 23 25 31 33
02 21 23 25 31 33 13 21 23 25 31 33
02 21 23 25 31 33 15 21 23 25 31 33
02 21 23 25 31 33 20 21 23 25 31 33
02 21 23 25 31 33 21 23 24 25 31 33
(51 行受影响)
在Cte,和Cte2中加上原本没有replace的字段不就行了啊,这么简单的问题
SELECT REPLACE([NOTEXT],' ','') AS [notext]