Solved

Count of perticular word in table

Posted on 2012-04-04
7
319 Views
Last Modified: 2012-04-09
I have table with columns (Id int, Userid int, LogData Varchar(500), Date DateTIme) containing data in LogData as,
Data
Data Mining
Web Mining
Website for data mining

I want a result as,
Word              Count
------------------------------------
Data                   3
Mining               3
Web                   1
Website             1
For                     1
0
Comment
Question by:swatiadeshpande
[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
7 Comments
 
LVL 9

Expert Comment

by:wasiftoor
ID: 37805360
A simple count aggregate function should do the job

I am copying the appropriate query here (Replace Table1 with the appropriate table name):

SELECT LogData as WORD, Count(LogData) as WordCount
FROM Table1
Group By LogData;

Open in new window

0
 
LVL 16

Accepted Solution

by:
Imran Javed Zia earned 500 total points
ID: 37805397
Hi,

This is thing is called as frequency of words. You better have to create list of tokenes/words from the given text in temp table or in memory table.
then you have to loop/cursor for each token against the text. you can use CharIndex(str1, str2, startIndex) funtion for this.


Thanks.
0
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 37805425
There is one more way to do this by using group by.
Create List of all tokens and insert into the memory table
Then query the memory table as following:


DECLARE @Tokens TABLE
(
      token  VARCHAR(500)      
)
 
 
 -- add all tokens into @Tokens, for this you can use newline, punctuations and space as delimeter

SELECT token, COUNT(*) AS Frequency
FROM @Tokens  
GROUP BY token
Order by Frequency
0
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
LVL 9

Expert Comment

by:sachinpatil10d
ID: 37806104
Try this

select DATA, COUNT(Data) Cnt from dbo.Split((SELECT LogData AS [data()] FROM tableName FOR XML PATH('')), ' ') group by Data

Open in new window


split function

CREATE FUNCTION dbo.Split
(
	@RowData nvarchar(2000),
	@SplitOn nvarchar(5)
)  
RETURNS @RtnValue table 
(
	Id int identity(1,1),
	Data nvarchar(100)
) 
AS  
BEGIN 
	Declare @Cnt int
	Set @Cnt = 1

	While (Charindex(@SplitOn,@RowData)>0)
	Begin
		Insert Into @RtnValue (data)
		Select 
			Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))

		Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
		Set @Cnt = @Cnt + 1
	End
	
	Insert Into @RtnValue (data)
	Select Data = ltrim(rtrim(@RowData))

	Return
END

Open in new window

0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 37806126
Here is another option:
declare @Table table (id int identity, LogData varchar(500))

insert into @Table values('Data')
insert into @Table values('Data Mining')
insert into @Table values('Web Mining')
insert into @Table values('Website for data mining')

Select Word, Count(1) Count
  From (
        select l.w.value('.','varchar(20)') Word
        from
        (
         Select cast('<w>' + replace(logdata, ' ', '</w><w>') + '</w>' as xml) as LogData 
           From @Table
        ) as t(Words)
        cross apply Words.nodes('w') l(w)
       ) as words
group by word

Open in new window

Output
Word                 Count
-------------------- -----------
Data                 3
for                  1
mining               3
Web                  1
Website              1

(5 row(s) affected)

Open in new window

0
 
LVL 9

Expert Comment

by:sachinpatil10d
ID: 37806149
Try this


select val, COUNT(val) cnt from (
select 
  r.value('.', 'nvarchar(100)') as Val
from (select cast('<r>'+replace((SELECT LogData AS [data()] FROM tableName FOR XML PATH('')), ' ', '</r><r>')+'</r>' as xml)) as x(x)
  cross apply
    x.nodes('r') as r(r)
)t
group by val

Open in new window

0
 
LVL 27

Expert Comment

by:tliotta
ID: 37808753
For the original question, would you ever need to count the word 'site'? Because "Website" contains 'site', are you concerned that it might be counted? (Or that it might be missed?)

Tom
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

749 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