SQL达人请进,高分求一个SQL查询的实现!!!
有一个文章表其中一个字段为关键字,存放关键字多个关键字用“|”隔开,如此 A|B|C|D 关键字有1-N个
我想实现根据文章ID,去查找有相同关键字最新的5条相关文章 并不包括查询条件的文章
[解决办法]
先合并分拆列 然后再分组TOP5
[解决办法]
-参考拆分表:--> --> (Roy)生成測試數據 if not object_id('Tab') is null drop table TabGoCreate table Tab([Col1] int,[COl2] nvarchar(5))Insert Tabselect 1,N'a,b,c' union allselect 2,N'd,e' union allselect 3,N'f'Go--SQL2000用辅助表:if object_id('Tempdb..#Num') is not null drop table #Numgoselect top 100 ID=Identity(int,1,1) into #Num from syscolumns a,syscolumns bSelect a.Col1,COl2=substring(a.Col2,b.ID,charindex(',',a.Col2+',',b.ID)-b.ID) from Tab a,#Num bwhere charindex(',',','+a.Col2,b.ID)=b.ID --也可用 substring(','+a.COl2,b.ID,1)=','--2000不使用辅助表Select a.Col1,COl2=substring(a.Col2,b.number,charindex(',',a.Col2+',',b.number)-b.number) from Tab a join master..spt_values b ON B.type='p' AND B.number BETWEEN 1 AND LEN(A.col2)where substring(','+a.COl2,b.number,1)=','SQL2005用Xml:select a.COl1,b.Col2from (select Col1,COl2=convert(xml,'<root><v>'+replace(COl2,',','</v><v>')+'</v></root>') from Tab)aouter apply (select Col2=C.v.value('.','nvarchar(100)') from a.COl2.nodes('/root/v')C(v))bSQL05用CTE:;with roy as (select Col1,COl2=cast(left(Col2,charindex(',',Col2+',')-1) as nvarchar(100)),Split=cast(stuff(COl2+',',1,charindex(',',Col2+','),'') as nvarchar(100)) from Tabunion allselect Col1,COl2=cast(left(Split,charindex(',',Split)-1) as nvarchar(100)),Split= cast(stuff(Split,1,charindex(',',Split),'') as nvarchar(100)) from Roy where split>'')select COl1,COl2 from roy order by COl1 option (MAXRECURSION 0)生成结果:/*Col1 COl2----------- -----1 a1 b1 c2 d2 e3 f*/
[解决办法]
----------------------------------- Author: liangCK 小梁-- Title : 查每个分组前N条记录-- Date : 2008-11-13 17:19:23-----------------------------------> 生成测试数据: #TIF OBJECT_ID('tempdb.dbo.#T') IS NOT NULL DROP TABLE #TCREATE TABLE #T (ID VARCHAR(3),GID INT,Author VARCHAR(29),Title VARCHAR(39),Date DATETIME)INSERT INTO #TSELECT '001',1,'邹建','深入浅出SQLServer2005开发管理与应用实例','2008-05-10' UNION ALLSELECT '002',1,'胡百敬','SQLServer2005性能调校','2008-03-22' UNION ALLSELECT '003',1,'格罗夫Groff.J.R.','SQL完全手册','2009-07-01' UNION ALLSELECT '004',1,'KalenDelaney','SQLServer2005技术内幕存储引擎','2008-08-01' UNION ALLSELECT '005',2,'Alex.Kriegel.Boris.M.Trukhnov','SQL宝典','2007-10-05' UNION ALLSELECT '006',2,'飞思科技产品研发中心','SQLServer2000高级管理与开发','2007-09-10' UNION ALLSELECT '007',2,'胡百敬','SQLServer2005数据库开发详解','2008-06-15' UNION ALLSELECT '008',3,'陈浩奎','SQLServer2000存储过程与XML编程','2005-09-01' UNION ALLSELECT '009',3,'赵松涛','SQLServer2005系统管理实录','2008-10-01' UNION ALLSELECT '010',3,'黄占涛','SQL技术手册','2006-01-01'--SQL查询如下:--按GID分组,查每个分组中Date最新的前2条记录--1.字段ID唯一时:SELECT * FROM #T AS T WHERE ID IN(SELECT TOP 2 ID FROM #T WHERE GID=T.GID ORDER BY Date DESC)--2.如果ID不唯一时:SELECT * FROM #T AS T WHERE 2>(SELECT COUNT(*) FROM #T WHERE GID=T.GID AND Date>T.Date)--SQL Server 2005 使用新方法--3.使用ROW_NUMBER()进行排位分组SELECT ID,GID,Author,Title,DateFROM( SELECT rid=ROW_NUMBER() OVER(PARTITION BY GID ORDER BY Date DESC),* FROM #T) AS TWHERE rid<=2--4.使用APPLYSELECT DISTINCT b.*FROM #T AS aCROSS APPLY( SELECT TOP(2) * FROM #T WHERE a.GID=GID ORDER BY Date DESC) AS b--结果/*ID GID Author Title Date---- ----------- ----------------------------- --------------------------------------- -----------------------003 1 格罗夫Groff.J.R. SQL完全手册 2009-07-01 00:00:00.000004 1 KalenDelaney SQLServer2005技术内幕存储引擎 2008-08-01 00:00:00.000005 2 Alex.Kriegel.Boris.M.Trukhnov SQL宝典 2007-10-05 00:00:00.000007 2 胡百敬 SQLServer2005数据库开发详解 2008-06-15 00:00:00.000009 3 赵松涛 SQLServer2005系统管理实录 2008-10-01 00:00:00.000010 3 黄占涛 SQL技术手册 2006-01-01 00:00:00.000(6 行受影响)*/
[解决办法]
--> 测试数据:[数据库]if object_id('[数据库]') is not null drop table [数据库]create table [数据库]([编号] int,[字段1] varchar(8),[字段2] varchar(11))insert [数据库]select 1,'AA|BB|CC','888|999|666' union allselect 2,'AA|BB|CC','111|222|333' union allselect 3,'AA|BB|CC','444|555|666'with T as ( select [编号],charindex('|','|'+[字段1])col1a, charindex('|',[字段1]+'|')+1 col1b, charindex('|','|'+[字段2]) col2a, charindex('|',[字段2]+'|')+1 col2b from [数据库] union all select a.编号,b.col1b,charindex('|',[字段1]+'|',b.col1b)+1, b.col2b,charindex('|',[字段2]+'|',b.col2b)+1 from [数据库] a join T b on a.编号=b.编号 where charindex('|',[字段1]+'|',b.col1b)>0 or charindex('|',[字段2]+'|',b.col2b)>0)select a.编号,[字段1]=substring(a.[字段1]+'|',b.col1a,b.col1b - b.col1a - 1),[字段2]=substring(a.[字段2]+'|',b.col2a,b.col2b - b.col2a - 1) into #test from [数据库] a join T b on a.编号=b.编号 order by 1 declare @str varchar(2000)set @str=''select @str=@str+','+[字段1]+'=max(case when [字段1]=' +quotename([字段1],'''')+' then [字段2] else 0 end)'from #testgroup by [字段1] exec('select 编号'+@str+' from #test group by 编号')/*编号AABBCC--------------------------------------188899966621112223333444555666*/
[解决办法]
--> 测试数据:[数据库]if object_id('[数据库]') is not null drop table [数据库]create table [数据库]([编号] int,[字段1] varchar(8),[字段2] varchar(11))insert [数据库]select 1,'AA|BB|CC','888|999|666' union allselect 2,'AA|BB|CC','111|222|333' union allselect 3,'AA|BB|CC','444|555|666'with T as ( select [编号],charindex('|','|'+[字段1])col1a, charindex('|',[字段1]+'|')+1 col1b, charindex('|','|'+[字段2]) col2a, charindex('|',[字段2]+'|')+1 col2b from [数据库] union all select a.编号,b.col1b,charindex('|',[字段1]+'|',b.col1b)+1, b.col2b,charindex('|',[字段2]+'|',b.col2b)+1 from [数据库] a join T b on a.编号=b.编号 where charindex('|',[字段1]+'|',b.col1b)>0 or charindex('|',[字段2]+'|',b.col2b)>0)select a.编号,[字段1]=substring(a.[字段1]+'|',b.col1a,b.col1b - b.col1a - 1),[字段2]=substring(a.[字段2]+'|',b.col2a,b.col2b - b.col2a - 1) into #test from [数据库] a join T b on a.编号=b.编号 order by 1 declare @str varchar(2000)set @str=''select @str=@str+','+[字段1]+'=max(case when [字段1]=' +quotename([字段1],'''')+' then [字段2] else 0 end)'from #testgroup by [字段1] exec('select 编号'+@str+' from #test group by 编号')/*编号 AA BB CC-----------------------------------------1 888 999 6662 111 222 3333 444 555 666*/
[解决办法]