Home All Groups Group Topic Archive Search About

select, a join with a count?

Author
23 Mar 2006 5:09 PM
HockeyFan
I've got 2 tables; basically, topics and messages.
How do I list the topics and then the count for each topic?

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


ALTER PROCEDURE [dbo].[udForumTopics]
    @CompanyID int
AS
SELECT
    T_ForumTopic.ForumTopicID AS "ForumTopicID",
    T_ForumTopic.TopicTitle AS "TopicTitle",
    T_ForumTopic.ActiveInd AS "TopicActive",
    T_ForumTopic.OwnerCompany AS "ForumTopicOwnerCompany",
    T_ForumTopic.CreateUser AS "ForumTopicCreateUser",
    T_ForumTopic.UpdateUser AS "ForumTopicUpdateUser",
    T_ForumTopic.CreateDate AS "ForumTopicCreateDate",
    T_ForumTopic.UpdateDate AS "ForumTopicUpdateDate",
    Count(ForumTopicMessageID) AS MessageCount
FROM [T_ForumTopic]
    INNER JOIN [T_ForumTopicMessage] ON [T_ForumTopicMessage].ForumTopicID =
[T_ForumTopic].ForumTopicID
WHERE T_ForumTopic.OwnerCompany = @CompanyID
GROUP By T_ForumTopic.ForumTopicID

This doesn't seem to work.  I thought I could do a count on
ForumtopicMessageID from the second table, but SQL Server Management Studio
says that
"Column 'T_ForumTopic.TopicTitle' is invalid in the select list because it
is not contained in either an aggregate function or the GROUP BY clause."

Author
23 Mar 2006 6:13 PM
Alejandro Mesa
Try,

> GROUP By T_ForumTopic.ForumTopicID

GROUP By
    T_ForumTopic.ForumTopicID,
    T_ForumTopic.TopicTitle,
    T_ForumTopic.ActiveInd,
    T_ForumTopic.OwnerCompany,
    T_ForumTopic.CreateUser,
    T_ForumTopic.UpdateUser,
    T_ForumTopic.CreateDate,
    T_ForumTopic.UpdateDate


AMB


Show quote
"HockeyFan" wrote:

> I've got 2 tables; basically, topics and messages.
> How do I list the topics and then the count for each topic?
>
> set ANSI_NULLS ON
> set QUOTED_IDENTIFIER ON
> go
>
>
> ALTER PROCEDURE [dbo].[udForumTopics]
>     @CompanyID int
> AS
> SELECT
>     T_ForumTopic.ForumTopicID AS "ForumTopicID",
>     T_ForumTopic.TopicTitle AS "TopicTitle",
>     T_ForumTopic.ActiveInd AS "TopicActive",
>     T_ForumTopic.OwnerCompany AS "ForumTopicOwnerCompany",
>     T_ForumTopic.CreateUser AS "ForumTopicCreateUser",
>     T_ForumTopic.UpdateUser AS "ForumTopicUpdateUser",
>     T_ForumTopic.CreateDate AS "ForumTopicCreateDate",
>     T_ForumTopic.UpdateDate AS "ForumTopicUpdateDate",
>     Count(ForumTopicMessageID) AS MessageCount
> FROM [T_ForumTopic]
>     INNER JOIN [T_ForumTopicMessage] ON [T_ForumTopicMessage].ForumTopicID =
> [T_ForumTopic].ForumTopicID
> WHERE T_ForumTopic.OwnerCompany = @CompanyID
> GROUP By T_ForumTopic.ForumTopicID
>
> This doesn't seem to work.  I thought I could do a count on
> ForumtopicMessageID from the second table, but SQL Server Management Studio
> says that
> "Column 'T_ForumTopic.TopicTitle' is invalid in the select list because it
> is not contained in either an aggregate function or the GROUP BY clause."
>

AddThis Social Bookmark Button