MSSQLTips.com - your daily source for SQL Server tips

Google
 
Web mssqltips.com

ESSENTIALS: Home | Tips | Search | Categories | Top 10 | Products | Authors | Blogs | Forums | Webcasts | Advertise | About
Best match query and order by columns -

in Search

Best match query and order by columns

Last post 05-18-2008 9:56 PM by ankitmathur. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 05-08-2008 1:09 AM

    Best match query and order by columns

    Hi,

    I'm trying to overcome a situation whereby I have to search through 4-5 columns and produce the results with an order by according to the values matched in these columns.

    Example:

    My Table Structure:

    Create Table TestPfx
    (
    pfx varchar(20),
    R1 money,
    R2 money,
    R3 money,
    R4 money,
    R5 money,
    )
    Insert into Testpfx values(1,1,7,1,3,9)
    Insert into Testpfx values(12,5,8,2,5,5)
    Insert into Testpfx values(123,8,9,3,7,1)
    Insert into Testpfx values(1234,3,4,4,1,7)
    Insert into Testpfx values(12345,6,5,5,5,5)
    Insert into Testpfx values(123456,9,6,6,9,3)
    Insert into Testpfx values(1234567,7,1,7,8,4)
    Insert into Testpfx values(12345678,4,2,8,5,8)
    Insert into Testpfx values(123456789,1,3,9,2,6)
    Select * from Testpfx


    Now, If I enter a value 124654654. I need this value to be best matched amongst those in my table. Like here value 12 will be best matched as we don't have a pfx value 124 in our table. had it been there it should've been my best match unless 1246 is present in pfx.

    I hope I'm clear with this point.

    Secondly, I need my output to be in an Ascending Order according to the column values for the selected best matched row. Like in our example, our best matched row is 12. So my output should look like this.

    Pfx  R3 R1 R4 R5 R2
    12   2    5  5  5   8

    If the searched number is 1230022827 result should be
    Pfx  R3 R1 R4 R5 R2
    123 1   3   7  8   9


    As part of my efforts so far I believe this should act as a query to BEST MATCH and find that single row. But I'm not too sure if its the best way.


    Declare @No varchar(20)
    Set @No = '124654654'
    Select top 1 * from Rates Where @No like Pfx + '%' order by Pfx Desc


    Please help as I'm totally getting clueless with arranging the columns in the best order.

    I hope I'm clear with my problem.

    Will look forward to the reply.

    Ankit Mathur

  • 05-18-2008 9:56 PM In reply to

    Re: Best match query and order by columns

    Hi all,

     While I was unable to get any response here. I got a very good response on another forum. For anybody who comes here looking for an answer to a similar query I'm posting a solution below.

     CREDITS: Peso and Vishakh 

     

    DECLARE @Search VARCHAR(20)

    SET @Search = '1230022827'

    SELECT TOP 1 t.*
    INTO  #Temp
    FROM  TestPfx AS t
    WHERE  @Search LIKE t.pfx + '%'
    ORDER BY LEN(t.pfx) DESC

    DECLARE @Temp TABLE (RowID INT IDENTITY(1, 1), pfx VARCHAR(20), Value MONEY)

    INSERT  @Temp
    SELECT  Value
    FROM  (
       SELECT pfx,
        R1 AS Value
       FROM #Temp

       UNION ALL

       SELECT pfx,
        R2
       FROM #Temp

       UNION ALL

       SELECT pfx,
        R3
       FROM #Temp

       UNION ALL

       SELECT pfx,
        R4
       FROM #Temp

       UNION ALL

       SELECT pfx,
        R5
       FROM #Temp
      ) AS d
    ORDER BY Value

    SELECT  pfx,
      MAX(CASE WHEN RowID = 1 THEN Value ELSE '' END) AS R1,
      MAX(CASE WHEN RowID = 2 THEN Value ELSE '' END) AS R2,
      MAX(CASE WHEN RowID = 3 THEN Value ELSE '' END) AS R3,
      MAX(CASE WHEN RowID = 4 THEN Value ELSE '' END) AS R4,
      MAX(CASE WHEN RowID = 5 THEN Value ELSE '' END) AS R5
    FROM  @Temp
    GROUP BY pfx

    DROP TABLE #Temp

     

Page 1 of 1 (2 items)