Solved

Count of perticular word in table

Posted on 2012-04-04
7
314 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unable to save view in SSMS 21 57
SQL Select Statement 2 21
Sql Query Datatype 2 18
conditional join based on column 4 0
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

912 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

25 Experts available now in Live!

Get 1:1 Help Now