Solved

FREETEXT and COUNT

Posted on 2006-11-20
3
410 Views
Last Modified: 2008-03-10
Hi I have a table that contains forum messages:

messageID       topicID       messageText

     1                    1           apple
     2                    1           orange
     3                    2           grape
     4                    1           cherry
     5                    3           peach

I have a query to get all the messages that contain the word "apple":

            SELECT * FROM tblMessages WHERE FREETEXT (messageText, 'apple');

This works fine but I would also like a count of how many other messages there are associated with the same topicID as the records returned by the the above query.  This is how I would like the result set to be:

messageID      topicID         messageText       numMsgs
      1                  1                 apple                   3

Is this possible?
0
Comment
Question by:champ_010
3 Comments
 
LVL 35

Accepted Solution

by:
Raynard7 earned 125 total points
ID: 17977687
SELECT
    tm.messageId,
    tm.topicId,
    (select count(*) from tblMessages as tm2 where tm2.topicId = tm.topicId) as numMsg
FROM
    tblMessages as tm
WHERE
    FREETEXT (tm.messageText, 'apple');
0
 
LVL 28

Expert Comment

by:imran_fast
ID: 17977707
SELECT
    tm.messageId,
    tm.topicId,
    No_Of_Msg
FROM
    tblMessages as tm
inner join (select count(*) No_Of_Msg , topicId from tblMessages group by topicId ) TC
on tc.topicId = tm.topicId
WHERE
    FREETEXT (tm.messageText, 'apple')
go
0
 
LVL 1

Author Comment

by:champ_010
ID: 17977773
Thanks!
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

685 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