Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Ordering FTS results by RANK

Posted on 2004-04-02
4
Medium Priority
?
814 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 800 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

721 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