I am using the following script to try and count the number of new comments and new discussions created in a group on our system between the last time you visited the group and now. I have written the following but cant get it to work:
SELECT TOP(6) a.GroupID , a. GroupName, sbuser.sf_NewGroupActivity( a.GroupID ,b.LastVisited) AS NumberUpdates
FROM Groups a
INNER JOIN GroupMembers b ON b .GroupID = a.GroupID
WHERE b. MemberID = 102
GROUP BY a.GroupID , a.GroupName, b.LastVisited
ORDER BY NumberUpdates ASC
and the sf script is as follows:
RETURNS INT
AS
BEGIN
DECLARE @OUTSTR INT
DECLARE @OUT1 INT
DECLARE @OUT2 INT
SET @OUT1 = (SELECT CAST(COUNT(GroupDiscussionsID) AS INT) FROM GroupDiscussions
WHERE MemberID = @GroupID AND CreateDate BETWEEN @LastVisited AND GetDate())
SET @OUT2 = (SELECT CAST(COUNT(GroupCommentID) AS INT) FROM GroupComments
WHERE MemberID = @GroupID AND CreateDate BETWEEN @LastVisited AND GetDate())
SET @OUTSTR = @OUT1 + @OUT2
RETURN @OUTSTR
END
I am at a loss as to how to get this to list the top 6 groups that have updates order by number of updates. Any ideas, suggestions, or solutions would be greatly appreciated. Many thanks
neojakey
I believe your problem is in the WHERE clause of your function:
Presumablyl, this should be comparing either MemberIDs or GroupIds, but not to each other.
However, I would strongly suggest that you remove the function call and create a single query for the counts. This allows the SQL to create a better execution plan. If you want to do this, and don’t know how, post another question.