[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.6

Access Search Query - Union, Distinct, and Order By Priority

Asked by Jahelka in SQL Query Syntax, Data Mining

Tags: access sql select union distinct order by priority

Goal:  Have a single text box where user is allowed to enter single or multiple search terms (seperated by spaces) and run query to return "search results" showing best matches first.

I have a table containing several pieces of information related to a person, and I'll use these four fields as an example: first name, last name, social security number, tax id number.  I want the user to be able to search (with one box) for any of these terms, and return exact matches first, followed by near matches second.  I'm generating this sql in vb.net by parsing the search box, splitting the terms on spaces, and then dynamically creating an sql statement.  I've got it working pretty good, but I'm not quite there yet.

The code block that follows helps illustrate what I'm trying to do (one term search of "Rob" in this example).  This query matches all Persons who's first name or last name IS just "Rob" and places those as a priority (=1), but it also selects all Persons who's first name STARTS WITH "Rob" and gives those a priority of 2.  So everything is great here, but now I get TWO records for both types of match (exact, and first letters).  I want to eliminate the duplicate matches, but still have the priority in the sort order.

I tried making this part of a sub query, as in the second example.  But Access doesn't like the combination of the DISTINCT along with the ORDER BY and a term that isn't included in the SELECT DISTINCT.  How do I get around this, or is there a better way to accomplish what I'm trying to do (I sure would like it to be all one query and not have to manually create a data table or temporary table to hold this information).

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
' Example #1
 
SELECT * FROM (
SELECT 1 as Priority, FirstName, LastName, SSN, TID FROM People 
WHERE FirstName='Rob' OR LastName='Rob' OR SSN='Rob' OR TID='Rob'
UNION ALL 
SELECT 2 as Priority, FirstName, LastName, SSN, TID FROM People
WHERE FirstName LIKE 'Rob%' OR LastName LIKE 'Rob%' OR SSN LIKE 'Rob%' OR TID LIKE 'Rob%'
) ORDER BY Priority, LastName, FirstName
 
' Example #2
 
SELECT DISTINCT FirstName, LastName, SSN, TID FROM (
SELECT 1 as Priority, FirstName, LastName, SSN, TID FROM People 
WHERE FirstName='Rob' OR LastName='Rob' OR SSN='Rob' OR TID='Rob'
UNION ALL 
SELECT 2 as Priority, FirstName, LastName, SSN, TID FROM People
WHERE FirstName LIKE 'Rob%' OR LastName LIKE 'Rob%' OR SSN LIKE 'Rob%' OR TID LIKE 'Rob%'
) ORDER BY Priority, LastName, FirstName
 
[+][-]11/02/09 09:45 PM, ID: 25726318Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: SQL Query Syntax, Data Mining
Tags: access sql select union distinct order by priority
Sign Up Now!
Solution Provided By: dqmq
Participating Experts: 1
Solution Grade: A
 
[+][-]11/02/09 07:48 PM, ID: 25725905Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/02/09 08:14 PM, ID: 25726005Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/02/09 10:44 PM, ID: 25726527Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/03/09 08:33 AM, ID: 25730690Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/03/09 08:38 AM, ID: 25730742Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92 - Hierarchy / EE_QW_3_20080625