Solved

Ordering FTS results by RANK

Posted on 2004-04-02
4
808 Views
Last Modified: 2008-02-26
Hi all ...

The following FTS SQL returns the results i need but the order of the results does not reflect the order of the sub-select statement results. How can i return the results in the exact order returned by the sub-select statment?

For example as it currently stands if the sub-select returns the IDs of (12, 5, 3, 8, 1) the final recordset will ignore that order and return the records in the order of (1, 3, 5, 8, 12).

SELECT l.ItemID, l.Name, l.CategoryID, l.SubCategoryID, l.FileName, c.Category, s.SubCategory
FROM tblSubCategories AS s
INNER JOIN tblLibraryItems AS l ON s.SubCategoryID = l.SubCategoryID
INNER JOIN tblCategories AS c ON l.CategoryID = c.CategoryID
WHERE l.ItemID IN
      (
      SELECT TOP 100 tblKeywordMatches.ItemID
      FROM tblKeywordMatches
      INNER JOIN FREETEXTTABLE(tblKeywords, Keyword, @SearchQuery) AS ft
      ON tblKeywordMatches.KeywordID = ft.[KEY]
      ORDER BY ft.Rank DESC
      )

Can anyone help? :)

Or alternatively can anyone think of a better way of doing this query?

Many thanks ...

eq






0
Comment
Question by:equentin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 13

Expert Comment

by:danblake
ID: 10740237
What happens if you specify the sub-query as:
 SELECT tblKeywordMatches.ItemID
     FROM tblKeywordMatches
     INNER JOIN FREETEXTTABLE(tblKeywords, Keyword, ''+@SearchQuery+'',100) AS ft
     ON tblKeywordMatches.KeywordID = ft.[KEY]

This should return the top 100 records within the sub-query of the free-text table, and order them by rank.

The last parameter within the freetexttable (object,search,top_n_by_rank) is top_n_by_rank

When an integer value, n, is specified, FREETEXTTABLE returns only the top n matches, ordered by rank.
0
 
LVL 4

Accepted Solution

by:
ATAHAC earned 200 total points
ID: 10740258
you can try this

SELECT l.ItemID, l.Name, l.CategoryID, l.SubCategoryID, l.FileName, c.Category, s.SubCategory
FROM tblSubCategories AS s
INNER JOIN tblLibraryItems AS l ON s.SubCategoryID = l.SubCategoryID
INNER JOIN tblCategories AS c ON l.CategoryID = c.CategoryID
INNER JOIN (
     SELECT TOP 100 tblKeywordMatches.ItemID AS ItemID,ft.Rank AS Rank
     FROM tblKeywordMatches
     INNER JOIN FREETEXTTABLE(tblKeywords, Keyword, ''+@SearchQuery+'') AS ft
     ON tblKeywordMatches.KeywordID = ft.[KEY]
     ORDER BY ft.Rank DESC
     )  AS k
ON k.ItemID = l.ItemID
ORDER BY k.Rank
0
 
LVL 13

Expert Comment

by:danblake
ID: 10740450
SELECT l.ItemID, l.Name, l.CategoryID, l.SubCategoryID, l.FileName, c.Category, s.SubCategory
FROM tblSubCategories AS s
INNER JOIN tblLibraryItems AS l ON s.SubCategoryID = l.SubCategoryID
INNER JOIN tblCategories AS c ON l.CategoryID = c.CategoryID
WHERE l.ItemID IN
     (
     SELECT TOP 100 tblKeywordMatches.ItemID
     FROM tblKeywordMatches
     INNER JOIN FREETEXTTABLE(tblKeywords, Keyword, @SearchQuery) AS ft
     ON tblKeywordMatches.KeywordID = ft.[KEY]
     ORDER BY ft.Rank DESC
     ) K
order by K.ItemID

or use the inner join as required.  
0
 

Author Comment

by:equentin
ID: 10740713
Thank you both for the help.

atahac's solution gave me what exactly i needed

0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question