2012-08-05 38 views
1

可能重複:
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 

回答

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

申報@Instring VARCHAR(2000)= '' 選擇@ Instring = @ Instring +」當標題像 '' % '+項目+' % '',然後 '' '+項目+' '' 'from dbo.split(@SearchItem,',') exec(' declare @tbl table(id int,title varchar(200)); with CTE as( select 1 as id,''Geo Prism GEO 1995 GEO * - ABS#16213899''作爲標題 union 選擇2作爲id,''Excavator JCB-ECU P/N:728/35700''作爲標題 union 選擇3作爲id,''Geo Prism GEO 1995 - ABS#162138 99'作爲標題 union 選擇4作爲id,'JCB挖掘機JCB- ECU P/N:728/35700'作爲標題 union 選擇5作爲id,'''Geo Prism GEO,GEO 1995 - ABS# 16213899 GEO '' 作爲標題 工會 選擇6爲ID, '' 馬魯蒂齒輪箱#ABS 4587 '' 作爲標題)

插入件插入@tbl選擇ID,從CTE標題

DECLARE @ TBL2表( id int,T int,title varchar(200),Tl varchar(200),CC int) insert into @tbl2(id,T,title,T1) select(id,T,title,Tl from(select tb.id ,sub.T,tb.title,Tl from(select case'+ @ Instring +'else''Other''END as Tl,cou nt(*)T from @tbl by case'+ @ Instring +'else''Other''END)Sub
join @tbl tb on tb.Title like''%''+ sub。TL + '' % '')Sub1的

申報@Ttle VARCHAR(最大) DECLARE @Tl VARCHAR(最大) 聲明@id INT 聲明@i INT = 0 DECLARE @Nbr INT = 0 聲明Cursr光標選擇標題,ID,鉈從@ tbl2的
開放Cursr 從旁邊取Cursr改成@ Ttle,@ ID,@鉈 而@@ FETCH_STATUS = 0 開始 而(@i < = LEN(@Ttle) ) 開始 如果charindex(@ Tl,@ Ttle)!= 0 set @Nbr = @ Nbr +1 set @Ttle = stuff(@ Ttle,ch arindex(@ T1中,@ Ttle),LEN(@Tl), '' '') 組@ I = @ i + 1的 端

 update @tbl2 set [email protected] where [email protected] 
    set @Nbr =0 

下從Cursr取入@ Ttle,@ ID,@ TL 端

選擇ID,T,標題,TL,從
CC(選擇ID,T,標題,TL,從@ CC TBL2
工會 選擇ID,0爲T,標題, ''」 '作爲Tl,0作爲來自@tbl的CC,其中id不在(從@ tbl2選擇id)S1 order by T desc,CC desc')

+0

這裏你用硬編碼搜索h術語,但我不想要硬編碼數據。 – Thomas 2012-08-07 09:14:15

+0

嗨,我編輯了代碼,請創建您在問題中編寫的拆分函數,並用您的表替換CTE – NikRED 2012-08-08 16:03:04