使用复杂的多重排序数据

时间:2012-08-05 18:04:43

标签: sql sql-server tsql

  

可能重复:
  Need help for complex data sort SQL Server

这是我在sql server中的数据,我需要排序和显示

Table : MyTable
------------------
ID  Title
-----------
1   Geo Prism GEO 1995 GEO* - ABS #16213899
2   Excavator JCB - ECU P/N: 728/35700
3   Geo Prism GEO 1995 - ABS #16213899
4   JCB Excavator JCB- ECU P/N: 728/35700
5   Geo Prism GEO,GEO 1995 - ABS #16213899 GEO
6   Maruti gear box #ABS 4587

现在我想基于搜索词对数据进行排序,如GEO& JCB

这些行将首先出现在GEO或JCB找到最长时间的位置 这里GEO发现有3行,JCB有2行。 因此所有行都有GEO关键字,它们将位于顶部,下一个JCB相关行将会出现。不匹配的行终将到来。

再次将进行排序。在GEO相关的行中......那些行将首先出现,它具有最大的GEO关键字。相同的JCB相关行将被排序。

这里我给的图像将显示我需要什么样的

enter image description here 我问了这个问题并得到了答案,并没有完全满足我的要求。所以这是我为这个问题得到的sql。

CREATE FUNCTION [dbo].[Split] (@String varchar(8000), @Delimiter char(1))     
returns @temptable TABLE (items varchar(8000))       
as       
begin       
declare @idx int       
declare @slice varchar(8000)       

select @idx = 1       
    if len(@String)<1 or @String is null  return       

while @idx!= 0       
begin       
   set @idx = charindex(@Delimiter,@String)       
   if @idx!=0       
       set @slice = left(@String,@idx - 1)       
   else       
      set @slice = @String       

   if(len(@slice)>0)  
       insert into @temptable(Items) values(@slice)       

   set @String = right(@String,len(@String) - @idx)       
   if len(@String) = 0 break       
end   
return       
end

DECLARE @Sterm varchar(MAX) 
SET @Sterm ='GEO JCB'
;WITH SearchResult (rnum, title)
as 
(   
(select 1 as rnum,'Geo Prism GEO 1995 GEO* - ABS #16213899' as title)
union all
(select 2 as rnum,'Excavator JCB - ECU P/N: 728/35700' as title)
union all
(select 3 as rnum,'Geo Prism GEO 1995 - ABS #16213899' as title)
union all
(select 4 as rnum,'JCB Excavator JCB- ECU P/N: 728/35700' as title)
union all
(select 5 as rnum,'Geo Prism GEO,GEO 1995 - ABS #16213899 GEO' as title)
union all
(select 6 as rnum,'dog' as title)
) 

select rnum, title from SearchResult
join 
( select lower(Items) as term 
  from dbo.Split(@Sterm , ' ')
) as search_terms
on lower(SearchResult.title) like '%' + search_terms.term +'%'
order by 
search_terms.term,
(select count(*)
from dbo.Split(lower(SearchResult.title),' ')
where Items = search_terms.term
) desc 

1 个答案:

答案 0 :(得分:0)

declare @SearchItem varchar(1000)='GEO,JCB'

声明@Instring varchar(2000)='' 选择@ Instring = @ Instring +'当标题为''%'+ items +'%''然后'''+ items +''''来自dbo.split(@ SearchItem,',') EXEC(” 声明@tbl表(id int,title varchar(200)); 以CTE为( 选择1作为id,''Geo Prism GEO 1995 GEO * - ABS#16213899''作为标题 联盟 选择2作为id,''挖掘机JCB - ECU P / N:728/35700'作为标题 联盟 选择3作为id,''Geo Prism GEO 1995 - ABS#16213899''作为标题 联盟 选择4作为id,''JCB挖掘机JCB-ECU P / N:728/35700'作为标题 联盟 选择5作为id,''Geo Prism GEO,GEO 1995 - ABS#16213899 GEO''作为标题 联盟 选择6作为id,''Maruti齿轮箱#ABS 4587'作为标题)

插入@tbl选择id,来自CTE的标题

声明@tbl2表(id int,T int,title varchar(200),Tl varchar(200),CC int) 插入@ tbl2(id,T,title,Tl) 选择id,T,title,Tl from(select tb.id,sub.T,tb.title,Tl from(select case'+ @Instring +'else''Other''END as Tl,count(*)T from @tbl group by case'+ @Instring +'else''Other''END)Sub
在tb上加入@tbl tb.Title就像''%''+ sub.Tl +''%'')Sub1

声明@Ttle varchar(max) 声明@Tl varchar(max) 声明@id int 声明@i int = 0 声明@Nbr int = 0 声明Cursr Cursor从@ tbl2中选择标题,id,T1 打开Cursr 从Cursr获取下一个@Ttle,@ id,@ Tl 而@@ FETCH_STATUS = 0 开始          while(@ i&lt; = len(@Ttle))          开始                      如果是charindex(@ Tl,@ Ttle)!= 0                      设置@Nbr = @ Nbr +1                      设置@Ttle = stuff(@ Ttle,charindex(@ Tl,@ Ttle),len(@Tl),'''')             设置@ i = @ i + 1          结束

     update @tbl2 set cc=@Nbr where id=@id 
     set @Nbr =0 

从Cursr获取下一个@Ttle,@ id,@ Tl 结束

从中选择id,T,title,T1,CC (从@ tbl2中选择id,T,title,Tl,CC 联盟 选择id,0为T,title,'''为Tl,0为来自@tbl的CC,其中id不在(从@ tbl2中选择id))S1顺序由T desc,CC desc')