存储过程中的 Group By 错误

发布于 2024-11-06 21:27:12 字数 1208 浏览 0 评论 0原文

我创建了这个存储过程,它基本上是返回一个办公室列表,其中包含每个办公室内发生的活动类型。我向 ReportViewer 报告了结果,但我注意到,对于每个活动返回,它都会创建一个表 - 所以我可以有 5 个不同的表,每个表都有自己的活动,但都发生在同一个办公室。我希望该报告成为每个办公室的表格,其中包含每个办公室的活动数量。所以我认为,如果我在存储过程中分组,我的结果将是我想要的,但我收到列错误:“...在选择列表中无效,因为它不包含在聚合函数或 GROUP 中BY 条款。”

我不知道该怎么做,但这是我的 select、from、where、group by 语句:

SELECT 
            O.OfficeId,
            O.OfficeName AS Office,
            HT.Description AS HearingType,
            H.HearingDate AS HearingDate,
            CR.Description AS Court,
            CT.[Description]AS CaseType
    FROM Activity H
    INNER JOIN ActivityEntry HE ON H.ActivityEntryId = HE.ActivityEntryId
    INNER JOIN ActivityType HT ON H.ActivityTypeId = HT.ActivityTypeId
    INNER JOIN [Case] C ON H.CaseId = C.CaseId
    INNER JOIN [Office] O ON HE.CreatedByOfficeId = O.OfficeId
    INNER JOIN [User] U ON C.CreatedByUserId = U.UserId
    LEFT OUTER JOIN CaseType CT ON C.CaseTypeId = CT.CaseTypeId
    LEFT OUTER JOIN Court CR ON C.CourtId = CR.CourtId
    WHERE .dbo.DateOnly(HE.HearingDate)BETWEEN @BeginDate AND @EndDate
    GROUP  BY
            O.OfficeId,
            O.OfficeName,
            HT.Description
    ORDER BY O.OfficeId, HT.Description

I created this stored procedure which is basically to return a list of offices with the type of activities that happen within each office. The results i reported to reportviewer but i noticed that for each activity return it creates a table - so i can have 5 different tables each with its own activity but all happen in the same office. I want the report to be a table for each office which will contain as many activites as there are for each office. So i thought that if i grouped in my stored procedure my results will be as what i want but i am getting column error saying: "...is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."

I am not sure how to go about that but here is my select, from, where, group by statements:

SELECT 
            O.OfficeId,
            O.OfficeName AS Office,
            HT.Description AS HearingType,
            H.HearingDate AS HearingDate,
            CR.Description AS Court,
            CT.[Description]AS CaseType
    FROM Activity H
    INNER JOIN ActivityEntry HE ON H.ActivityEntryId = HE.ActivityEntryId
    INNER JOIN ActivityType HT ON H.ActivityTypeId = HT.ActivityTypeId
    INNER JOIN [Case] C ON H.CaseId = C.CaseId
    INNER JOIN [Office] O ON HE.CreatedByOfficeId = O.OfficeId
    INNER JOIN [User] U ON C.CreatedByUserId = U.UserId
    LEFT OUTER JOIN CaseType CT ON C.CaseTypeId = CT.CaseTypeId
    LEFT OUTER JOIN Court CR ON C.CourtId = CR.CourtId
    WHERE .dbo.DateOnly(HE.HearingDate)BETWEEN @BeginDate AND @EndDate
    GROUP  BY
            O.OfficeId,
            O.OfficeName,
            HT.Description
    ORDER BY O.OfficeId, HT.Description

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

呢古 2024-11-13 21:27:12

GROUP BY 要求您的列列表中具有某种聚合函数 - SUMAVGCOUNTGROUP BY 仅与聚合结合使用才有意义。

否则,只需使用 ORDER BY 语句对数据进行排序即可。

GROUP BY requires that you have some kind of an aggregate function in your list of columns - a SUM, an AVG, a COUNT. GROUP BY only makes sense in combination with an aggregate.

Otherwise, just simply order your data with an ORDER BY statement.

楠木可依 2024-11-13 21:27:12

您没有使用任何聚合函数(无论如何乍一看),因此您不需要 group by 子句。您可以在 order by 中完成所有排序,然后在应用程序端处理时将其提取到不同的数据集中。

示例:

select ... from ... order by OfficeID, Description

这将返回所有办事处的单个结果。现在您需要在代码中解析它,

int OfficeID=-1;
while(recordset.moveToNextRow())
{
    if(currentRecord.OfficeID!=OfficeID)
    {
        //This is a new office, do whatever you need to do to split the data up here
        OfficeID=currentRecord.OfficeID;
    }
    //Process the record as a part of the current office here
}

因此,如果您在网页上构建一个表,则每次您点击新的办公室 ID 时,您可能会结束最后一个表并启动一个新表。这里您需要一些额外的逻辑,但这应该会给您带来想法。

请注意,您的问题与使用存储过程无关,而与您如何选择和处理数据有关。

You aren't using any aggregate functions (on first glance anyway) so you don't need a group by clause. You can do all your ordering in the order by and then extract it into different datasets as you process it on the application side.

Example:

select ... from ... order by OfficeID, Description

This returns a single result for all offices. Now you need to parse it in code

int OfficeID=-1;
while(recordset.moveToNextRow())
{
    if(currentRecord.OfficeID!=OfficeID)
    {
        //This is a new office, do whatever you need to do to split the data up here
        OfficeID=currentRecord.OfficeID;
    }
    //Process the record as a part of the current office here
}

So if you were building a table on a webpage, you'd maybe end the last table and start a new table every time you hit a new office ID. There's some additional logic you'll need here, but this should give you the idea.

Note that your problem has nothing to do with using a stored procedure and everything to do with how you are selecting and processing data.

塔塔猫 2024-11-13 21:27:12

我实际上意识到我的问题无法通过一些成员提到的存储过程来解决。由于我在报告中显示结果,因此我重新组织了报告和数据集信息,以便存在父子关系,并且从数据集中我的信息得到了正确的组织。我使用这篇文章提供的解决方案来帮助指导我:用于帮助指导我的帖子。< /a>

I actually realized that my problem cannot be solved through my stored procedure as mentioned from some of the members. Since i am displaying results in my report so i re-organized my report and dataset information so that there is a parent and child relationship and from the dataset my information was organized properly. I used the solutions offered from this post to help guide me: post used to help guide me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文