SQL 按月分组

发布于 2024-12-20 01:23:42 字数 564 浏览 1 评论 0原文

参见图片。

在我的报告中,我需要每个“项目名称”的总成本、小时数、天数。

出于某种原因,我无法查看按月对每个项目的所有数据进行分组,例如,如果您看到 2010 年 7 月有 2 个条目,我希望将其显示为一行上的总计。谁能帮助我。

SELECT     TOP (100) PERCENT Date_year, Date_month_number, Date_month_name, Service, Hours, Days, ProjectName, Cost
FROM         dbo.Resources_group_projectname
WHERE     (Service LIKE '%Housing%')
GROUP BY ProjectName, Date_year, Date_month_number, Date_month_name, Hours, Days, Cost, Service
ORDER BY Date_year, Date_month_number

在此处输入图像描述

See image.

In my report I need to have total Costs, Hours, Days for each 'ProjectName'

For some reason, I cannot get my view to Group all the data for each Project by Month, for example if you see July 2010 has 2 entries, I want this to be displayed as totals on one line. Can anyone help me.

SELECT     TOP (100) PERCENT Date_year, Date_month_number, Date_month_name, Service, Hours, Days, ProjectName, Cost
FROM         dbo.Resources_group_projectname
WHERE     (Service LIKE '%Housing%')
GROUP BY ProjectName, Date_year, Date_month_number, Date_month_name, Hours, Days, Cost, Service
ORDER BY Date_year, Date_month_number

enter image description here

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

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

发布评论

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

评论(2

攒一口袋星星 2024-12-27 01:23:42

您的列列表不包含任何聚合。请尝试以下操作:

SELECT Date_year, Date_month_number, Date_month_name, Service, SUM(Hours), SUM(Days), ProjectName, SUM(Cost)
FROM dbo.Resources_group_projectname
WHERE (Service LIKE '%Housing%')
GROUP BY ProjectName, Date_year, Date_month_number, Date_month_name, Service
ORDER BY Date_year, Date_month_number

Your list of columns does not contain any aggregates. Try the following:

SELECT Date_year, Date_month_number, Date_month_name, Service, SUM(Hours), SUM(Days), ProjectName, SUM(Cost)
FROM dbo.Resources_group_projectname
WHERE (Service LIKE '%Housing%')
GROUP BY ProjectName, Date_year, Date_month_number, Date_month_name, Service
ORDER BY Date_year, Date_month_number
美人迟暮 2024-12-27 01:23:42

当您不仅按项目和月份分组,特别是按小时和天分组时,就会出现重复项。

因此,您应该按输出中实际想要的每行键进行分组,例如 Project_Name、Date_Year 和 Date_Month_Number。您显示的每个其他字段都应该是聚合字段,例如 sum(hours)

Your duplicates come about as your are grouping by more than just project and month, specifically by including hours and days.

You should therefore group by what you actually want as the key per line in your output, likely Project_Name, Date_Year and Date_Month_Number. Every other field you display should be an aggregate field, e.g. sum(hours)

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