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
Solved

Count of perticular word in table

Posted on 2012-04-04
7
318 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
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

856 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