0nir 0nir - 1 year ago 56
SQL Question

Sql: Multiple keywords search for complaints data set

I am working on a complaints data-set & I am looking for matching keywords from the keyword dictionary against the data-set.

Here is the keyword dictionary -

Sample Keyword Dictionary


Debit Card

Credit Card



Sample data-set -

enter image description here

And, Sample Output -

enter image description here

That is, I would like the keywords from the keyword dictionary to be matched against the "complaint description" column in my data-set. And, I would like only the matched keywords to show up in the "Keywords Matched" column & count of total keywords matched in the "Count" column.

This is just a sample & my actual data-set is huge plus it might involve "Complaint Description" in foreign language as well. Can you help me on how to go about it?

Thanks a lot!!

xbb xbb
Answer Source

Assume your sample data table is [Sample Data-set] and your have your keywords saved in a table called [Keyword] (with column name Keyword). Try this:

SELECT Detail.*, [Keywords Matched] = STUFF((select ',' + Keyword
    WHERE Detail.[Complaint Description] like '%' + b.Keyword + '%'
    ORDER BY Keyword DESC
    FOR XML PATH('')), 1,1,'')
FROM [Sample Data-set] as Detail
GROUP BY Detail.BranchID, Detail.Portfolio, Detail.[Complaint Description]
SELECT *, len([Keywords Matched]) - len(replace([Keywords Matched], ',','')) + 1 as [Count]
GROUP BY  BranchID, Portfolio, [Complaint Description], [Keywords Matched]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download