[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Return percentage match when perform an SQL Query

Posted on 2000-02-18
11
Medium Priority
?
1,246 Views
Last Modified: 2008-10-04
Dear Expert
How do i do an SQL query that will search through a database and return all data that match at least one of the variables being search. The query must also sort the data into order from 100% match (all variables) down. ie similary to what most web sites use when searching databases.
0
Comment
Question by:bornfree
  • 3
  • 2
  • 2
  • +3
11 Comments
 
LVL 12

Expert Comment

by:Trygve
ID: 2534563
How would you like the percentages to be calucated? According to how many times the word(s) appear in the text or just based on how many of the that match. The first one will most likely force you to make a "statistics" table that counts the words per article so that you can use this in the calculations. If you are to count them "on the fly", your query will be slooooooow.
0
 

Author Comment

by:bornfree
ID: 2534607
I will not be searching a text document. Assume the main database consists a number of fields, each field contains specific info. if i would like to do a search and reflect the percentage of matches vs total i have searched on.

ie lets say i have a database of employees, with fields for gender, height, hair colour etc. I would then like to do a search for male, blonde hair and brown eyes. I would therefore like my query to return in order of 100%, 67%, 33%

hpe this makes more sence.
0
 
LVL 12

Expert Comment

by:Trygve
ID: 2534655
SELECT Match1: iif([Gender]='Male',1,0), Match2: iif([Hair Color]='Blonde',1,0), Match3: Iif([Eye Color]='Brown',1,0) FROM TableName

Now you can do the calculation on your form/report  =([Mathc1]+[Match2]+[Match3])/3

or change the query

SELECT Match1: iif([Gender]='Male',1,0), Match2: iif([Hair Color]='Blonde',1,0), Match3: Iif([Eye Color]='Brown',1,0), FoundPercentage: [Match1]+[Match2]+[Match3] FROM TableName

If this is not valid (and I am not sure if it is, then try this:

SELECT Match1: iif([Gender]='Male',1,0), Match2: iif([Hair Color]='Blonde',1,0), Match3: Iif([Eye Color]='Brown',1,0), FoundPercentage: iif([Gender]='Male',1,0) + iif([Hair Color]='Blonde',1,0) + Iif([Eye Color]='Brown',1,0) FROM TableName
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 7

Expert Comment

by:Dedushka
ID: 2534681
Hi bornfree.
You can use DCount aggregate function:

Dim intTotalEmployees
Dim intMale as Integer
Dim intMaleBlonde as Integer
Dim intMaleBlondeAndBrownEyes as Integer

intTotalEmployees = Dcount("EmpId", "Employees")
intMale = DCount("EmpId", "Employees", "[Gender]='male'")
intMaleBlonde = DCount("EmpId", "Employees", "[Gender]='male' And [Hair Color] = 'Blonde'")
intMaleBlondeAndBrownEyes = ("EmpId", "Employees", "[Gender]='male' And [Hair Color] = 'Blonde' And [Eyes Color] = 'Brown'")

Debug.Print "Total - " & intTotalEmployees & " (100%)"
Debug.Print "Males - " & intMale & " (" & Int(intMale /intTotalEmployees *100) & "%)"
intTotalEmployees & " (100%)"
Debug.Print "Blonde - " & intMaleBlonde & " (" & Int(intMaleBlonde /intMale *100) & "%)"
Debug.Print "Brown Eyes - " & intMaleBlondeAndBrownEyes & " (" & Int(intMaleBlondeAndBrownEyes /intMaleBlonde *100) & "%)"

Regards,
Dedushka
0
 
LVL 12

Expert Comment

by:Trygve
ID: 2534903
Dedushka: The task here seems to be to list all the records that match and then sort them according to the hit-percentage. Using the DCounts would be nice if you wondered about the total percentages.
0
 
LVL 10

Accepted Solution

by:
paasky earned 300 total points
ID: 2535282
Hello bornfree,

You could try something like this:

SELECT *,
((E.HairColor=[Hair color: ])*-1 +
 (E.Gender=[Gender: ])*-1 +
 (E.EyeColor=[Eye color: ])*-1
) / 3 * 100 AS [Match Percentage]
FROM tblEMPLOYEE AS E;

You can add as many criteria you need and divide the number with number of criterias.

Regards,
Paasky
0
 
LVL 3

Expert Comment

by:tomk120999
ID: 2535603
Just a variation on the above comments.  This selects the match fields and calculates the percent match in one field (MatchPct) and then orders the records by that percent descending:

SELECT Name, Gender, HairColor, EyeColor, IIf([Gender] = "Male", 1/3*100,0) + IIf([HairColor] = "Blonde", 1/3*100,0) + IIf([EyeColor] = "Blue", 1/3*100,0) as MatchPct FROM Table WHERE Gender = "Male" OR HairColor = "Blonde" OR Eyecolor = "Blue" ORDER BY MatchPct desc;

To use it just change the table and field names to the ones your DB uses and change the "1/3*100" calculation to however many criteria you use.  For instance, if you used 5 criteria, then the calculation would be "1/5*100".
HTH, good luck as usual...
0
 
LVL 3

Expert Comment

by:tomk120999
ID: 2535633
BTW, bornfree, tha above will select all males, all blondes (male *and* female), and all blue eyes (male *and* female).  I hope that's what you had in mind?
good luck as usual...
0
 
LVL 31

Expert Comment

by:hnasr
ID: 2536407
'bornfree,

' Try this query.  It references a function as bellow.

SELECT tbl.fld1, tbl.fld2, tbl.fld3, percent_match([fld1],"male",[fld2],"blonde hair",[fld3],"brown eyes") AS myMatch
FROM tbl
ORDER BY percent_match([fld1], "male",[fld2], "blonde hair", [fld3], "brown eyes") DESC;


' here is the function:

Public Function percent_match(f1, c1, f2, c2, f3, c3 As String) As Integer
' f stands for field, c stands for criteria
    Dim myMatch As Integer
    myMatch = 0
    If f1 = c1 Then
        myMatch = myMatch + 1
    End If
    If f2 = c2 Then
        myMatch = myMatch + 1
    End If
    If f3 = c3 Then
        myMatch = myMatch + 1
    End If
   
    Select Case myMatch
        Case 1
            percent_match = 33
        Case 2
            percent_match = 66
        Case 3
            percent_match = 100
        Case Else
            percent_match = 0
    End Select
End Function

' you may use constants instead of the numbers.
0
 

Author Comment

by:bornfree
ID: 2546493
Why do you multiply the selection by -1 for each search variable?
0
 
LVL 10

Expert Comment

by:paasky
ID: 2548083
bornfree:

True = -1, and if we want positive percent value, we must multiply condition return value by -1 (-1 * -1 = 1).

You're right that it's not necessary to do the multiplying for all values - this should do the same trick:

SELECT *,
(((E.HairColor=[Hair color: ]) +
 (E.Gender=[Gender: ]) +
 (E.EyeColor=[Eye color: ])) * -1
) / 3 * 100 AS [Match Percentage]
FROM tblEMPLOYEE AS E
WHERE ...

Regards,
Paasky
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month19 days, 17 hours left to enroll

872 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