i have a table which has many fields but i want to get count of every word in any three fields of that table
find all title in a table that exist more than once…so for that i can issue this statement
SELECT title, COUNT(title) AS NumOccurrences FROM users
GROUP BY titleHAVING ( COUNT(title) > 1 )
suppose my table has three fields called title,url,description.
basically i do not know which word has been stored in which 3 fields in that table maximum time.
i want to issue a sql statement which can show me which word found maximum time…like
word-name occurance
--------- -------
sqlserver 300
jquery 120
ajax 110
please guide me with sample sql for sql server 2000/2005 thanks
Here is my updated full code…..please have look
IF OBJECT_ID('tempdb..#tempSearch') IS NOT NULL
BEGIN
DROP TABLE #tempSearch
END
CREATE TABLE #tempSearch(
ID INT,
Title nvarchar(4000),
Description ntext,
Url nvarchar(4000),
Type char(1))
INSERT INTO #tempSearch
SELECT * from vwProductSearch
INSERT INTO #tempSearch
SELECT * from vwContentSearch
SELECT Word,
COUNT(Word) AS TotalOccurrences,
COUNT(CASE WHEN Field = 'Title' THEN Word END) AS OccurancesInTitle,
COUNT(CASE WHEN Field = 'URL' THEN Word END) AS OccurancesInURL,
COUNT(CASE WHEN Field = 'Description' THEN Word END) AS OccurancesInDescription
FROM ( SELECT CONVERT(NTEXT, Title) AS Word, 'Title' AS Field
FROM #tempSearch
UNION ALL
SELECT CONVERT(NTEXT, URL), 'URL' AS Field
FROM #tempSearch
UNION ALL
SELECT CONVERT(NTEXT, Description), 'Description' AS Field
FROM #tempSearch
) As Fields
GROUP BY Word
HAVING COUNT(Word) > 1
DROP TABLE #tempSearch
You need to use UNION to combine your 3 fields into a single column so you can use this to group by. I’ve also added a few more counts in case you need to drill down as to where the word occurs the most.
EDIT
I know you have asked about SQL_Server 2005 and 2000, but if you were ever to upgrade to 2008 or later there is a much cleaner solution:
EDIT 2
If all your columns are different datatypes you will need to explicitly convert them:
EDIT 3
There is no way around the error you are getting, you cannot group by NTEXT. The best solution I can come up with feels very dirty, and I’m not particularly happy with it…
This works, but like I said, it isn’t perfect and probably won’t perform very well. Strongly consider upgrading to a later version of SQL-Server!