如何按月进行SUM和组数据

时间:2014-09-30 13:54:28

标签: sql oracle sql-view

我正在Oracle 10g中创建一个View。有关我的数据的示例,请参见附图。我希望能够对具有相同ProjectCode和Month的所有记录执行SUM。我在下面的查询仍然显示了单独列出的每个ProjectCode的个别月份:

SELECT ProjectCode,  ItemNumber, ItemCode, Type, SUM(Estimate), Month
FROM ProjectDetails
<bunch of INNER JOINS HERE>
WHERE ProjectDetails.ProjectCode=AllDetails.ProjectCode 
GROUP BY ProjectCode,  ItemNumber, ItemCode, Type, Estimate, Month;

enter image description here

1 个答案:

答案 0 :(得分:2)

group by你想看的东西。从selectgroup by

中删除无关列
SELECT ProjectCode, SUM(Estimate), Month
FROM ProjectDetails
<bunch of INNER JOINS HERE>
WHERE ProjectDetails.ProjectCode=AllDetails.ProjectCode 
GROUP BY ProjectCode,  Month;

编辑:

也许这就是你想要的:

SELECT ProjectCode,  ItemNumber, ItemCode, Type, SUM(Estimate), Month
FROM ProjectDetails
<bunch of INNER JOINS HERE>
WHERE ProjectDetails.ProjectCode=AllDetails.ProjectCode 
GROUP BY ProjectCode,  ItemNumber, ItemCode, Type, Month;

注意:我所做的就是从estimate删除group by

您还应该将连接条件放在from子句中,而不是where子句中。