Solved

Count of perticular word in table

Posted on 2012-04-04
7
312 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
7 Comments
 
LVL 9

Expert Comment

by:wasiftoor
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 9

Expert Comment

by:sachinpatil10d
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now