Solved

SQL Query to find keywords and count

Posted on 2014-02-27
3
382 Views
Last Modified: 2014-03-30
Looking for a good sql query that will pull out keywords from a paragraph and count how many times they occur.

For example:

KeywordTable:

Word
the
and
a
like

Paragraph:
Conscientiousness is one of the five traits of the Five Factor Model of personality, and is an aspect of what has traditionally been referred to as having character. Conscientious individuals are generally hard working and reliable. When taken to an extreme, they may also be "workaholics", perfectionists, and compulsive in their behavior. People who score low on conscientiousness tend to be more laid back, less goal-oriented, and less driven by success; they also are more likely to engage in antisocial and criminal behavior.

Outcome:
the, 5
and, 3
etc.

SELECT Word, COUNT(Word) AS count FROM KeywordTable WHERE....(any of the words in the keyword table are in the paragraph and their count)

Thanks in advance!!!

D-
0
Comment
Question by:MaxKroy
[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
3 Comments
 
LVL 40

Expert Comment

by:lcohan
ID: 39892886
I guess you could use this sample code below and plug in your keywords to get something working for you - I know is not complete answer but hopefully more than nothing...

DECLARE @Text AS VarChar(MAX)
SET @Text = 'This is a sentence'
PRINT  'Number of words: ' + CAST(LEN(@Text) - LEN(REPLACE(@Text, ' ', '')) +1 AS VarChar(3))
0
 
LVL 35

Expert Comment

by:David Todd
ID: 39896901
Hi,

Generally SQL is the wrong tool for this. You can use a hammer to pound in screws, and a screwdriver to pound nails, but neither is very successful at that.

In what form would the paragraph be? Individual words in a table, or as a one value in a varchar( max ) column or variable?

Maybe a CLR function using the .net libraries for string manipulation would be better here.

HTH
  David
0
 
LVL 41

Accepted Solution

by:
Sharath earned 500 total points
ID: 39927587
try this query.
declare @Paragraph varchar(max)
select @Paragraph = 'Conscientiousness is one of the five traits of the Five Factor Model of personality, and is an aspect of what has traditionally been referred to as having character. Conscientious individuals are generally hard working and reliable. When taken to an extreme, they may also be "workaholics", perfectionists, and compulsive in their behavior. People who score low on conscientiousness tend to be more laid back, less goal-oriented, and less driven by success; they also are more likely to engage in antisocial and criminal behavior.'

;with  cte0 as (select 1 as c union all select 1),        
       cte1 as (select 1 as c from cte0 a, cte0 b),        
       cte2 as (select 1 as c from cte1 a, cte1 b),        
       cte3 as (select 1 as c from cte2 a, cte2 b),        
       cte4 as (select 1 as c from cte3 a, cte3 b),        
       nums as (select row_number() over (order by c) as n from cte4)       
SELECT kt.Word,COUNT(*) cnt
  FROM KeywordTable kt 
       JOIN (SELECT n,LTRIM(SUBSTRING(Paragraph, n, CHARINDEX(' ', Paragraph + ' ', n) - n)) AS Word
               FROM nums 
                    CROSS JOIN (SELECT @Paragraph Paragraph) T1 
              WHERE SUBSTRING(' ' + Paragraph, n, 1) = ' ' 
                AND n < LEN(Paragraph) + 1) p 
         ON kt.Word = p.Word 
 GROUP BY kt.Word 

Open in new window

http://sqlfiddle.com/#!3/59cf12/3
0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
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…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

623 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