Need help for complex data sort SQL Server

后端 未结 2 1239
广开言路
广开言路 2021-01-24 03:06

I need help for complex data sort from database. Suppose my data stored in table like this:

Description
--------------
JCB Excavator - ECU P/N: 728/35700
Geo Pri         


        
相关标签:
2条回答
  • 2021-01-24 03:33

    Here you go

    declare @t table(Description varchar(1000))
    insert into @t
    select 'JCB Excavator - ECU P/N: 728/35700 ' union all
    select 'Geo Prism 1995 GEO - ABS #16213899 GEO pump ' union all
    select 'Geo Prism 1995 - GEO ABS #16213897 ' union all
    select 'Geo Prism 1995 - ABS #16213897 ' union all
    select 'Ersatz Airbags, Gurtstrammer und Auto Körper  Teile ' union all
    select 'this test JCB pipe & JCB pump ' union all
    select 'Wie man BBA reman erreicht'
    
    declare @search_term varchar(100)
    set @search_term ='GEO'
    select Description from @t
    order by len(Description)-len(replace(Description,@search_term,'')) desc
    

    Result

    Description
    ----------------------------------------------------------
    Geo Prism 1995 GEO - ABS #16213899 GEO pump 
    Geo Prism 1995 - GEO ABS #16213897 
    Geo Prism 1995 - ABS #16213897 
    Ersatz Airbags, Gurtstrammer und Auto Körper  Teile 
    this test JCB pipe & JCB pump 
    Wie man BBA reman erreicht
    JCB Excavator - ECU P/N: 728/35700 
    
    0 讨论(0)
  • 2021-01-24 03:37
    1. Write a function to tokenize a string into a table of words. eg: How do I split a string so I can access item x?
    2. Apply this function to the search string and the description table
    3. Join the two results with cross apply; group the results, count and sort.
    0 讨论(0)
提交回复
热议问题