我有一个有很多字段的表,但我想计算该表的任何三个字段中的每个单词
在一个表中找到不止一次存在的所有标题......所以我可以发出这个声明
SELECT title, COUNT(title) AS NumOccurrences FROM users
GROUP BY titleHAVING ( COUNT(title) > 1 )
假设我的表有三个名为title,url,description的字段。基本上我不知道哪个单词已存储在该表的哪个 3 个字段中的最大时间。
我想发出一条 sql 语句,它可以告诉我哪个单词找到了最长时间……比如
word-name occurance
--------- -------
sqlserver 300
jquery 120
ajax 110
请指导我使用 sql server 2000/2005 的示例 sql 谢谢
这是我更新的完整代码.....请看
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