is there any way i can do a query to specify that I want to give priority to some value?
for instance i have:
SELECT TOP (20)
r.MD5, r.Title, r.Link, t.Category, t.tfidf, COUNT(r.MD5) AS matching_terms
FROM
Resource AS r INNER JOIN tags AS t ON r.MD5 = t.MD5
WHERE
(t.Category IN ('algorithm', 'k-means', 'statistics', 'clustering', 'scien开发者_Go百科ce'))
GROUP BY r.MD5, r.Title, r.Link, t.Category, t.tfidf
ORDER BY matching_terms DESC, t.tfidf DESC
i want that 'algorithm' is given higher priority when finding results. any ideas?
I'm not sure how high a priority you want to make 'algorithm'
, but in any case, you can add this to the ORDER BY
clause, in order to make it the most important category (all other categories are equally important):
ORDER BY ..., CASE t.Category = 'algorithm' THEN 0 ELSE 1 END, ...
If however your concept of "priority" is somehow correlated with the importance of the matching_terms
expression, you could also try something like this (you'd have to nest your above select)
SELECT TOP(20) FROM (
[your original select without TOP(20) clause]
)
ORDER BY (matching_terms * CASE t.Category = 'algorithm'
THEN 1.5 ELSE 1 END) DESC, t.tfidf DESC
But that's just an example to give you an idea.
UPDATE: Following you comment, you can generate a case statement like this:
ORDER BY CASE t.Category WHEN 'algorithm' THEN 0
WHEN 'k-means' THEN 1
WHEN 'statistics' THEN 2
WHEN 'clustering' THEN 3
WHEN 'science' THEN 4 END
Or alternatively (especially if your list of categories is large), then you should add a sort
field to tags, containing the priority. Then you could simply order by sort
SELECT TOP (10) r.MD5, r.Title, r.Link, t.Category, t.tfidf, COUNT(r.MD5) AS matching_terms
FROM Resource AS r INNER JOIN
tags AS t ON r.MD5 = t.MD5
WHERE (t.Category IN ('astrophysics', 'athletics', 'sports', 'football', 'soccer'))
GROUP BY r.MD5, r.Title, r.Link, t.Category, t.tfidf
ORDER BY (CASE t .Category WHEN 'astrophysics' THEN 0 WHEN 'athletics' THEN 1 WHEN 'sports' THEN 2 WHEN 'football' THEN 3 WHEN 'soccer' THEN 4 END)
Thanks for giving me the idea Lukas Eder
精彩评论