SQL 按月分组
参见图片。
在我的报告中,我需要每个“项目名称”的总成本、小时数、天数。
出于某种原因,我无法查看按月对每个项目的所有数据进行分组,例如,如果您看到 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的列列表不包含任何聚合。请尝试以下操作:
Your list of columns does not contain any aggregates. Try the following:
当您不仅按项目和月份分组,特别是按小时和天分组时,就会出现重复项。
因此,您应该按输出中实际想要的每行键进行分组,例如 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)