我在下面的查询导致了我的问题。总而言之,因为我有不同的服务日期,它由不同的服务公司进行分组,但我只想要明确的区域接下来的总数。因为我现在很困惑,怎么能绕过这个!!!
SELECT DISTINCT(DISTRICT),
Sum(completed) as TotalCompleted,
MONTHNAME,
MONTH
FROM( SELECT TBL_PROPERTY.DISTRICT,
TBL_SERVICE.SERVICEDATE,
DATENAME(MONTH, SERVICEDATE) AS MONTHNAME,
CONVERT(INT,CONVERT(VARCHAR,DATEPART(YEAR, SERVICEDATE) + '00')) + DATEPART(MONTH, SERVICEDATE) AS MONTH,
COUNT(tbl_property.Propref) AS Completed
FROM TBL_SERVICE INNER JOIN TBL_PROPERTY ON TBL_SERVICE.PROPREF = TBL_PROPERTY.PROPREF
WHERE (TBL_PROPERTY.CONTRACT ='ma2') AND
(LASTSERVICEDATE BETWEEN '01/jun/2009' AND DATEADD(hh,23,'15/jun/2009')) AND
(NOT (TBL_SERVICE.BILLCODE1 = 'NA')) AND
(TBL_SERVICE.STATUS = 'Serviced') AND
(PROPERTYCLASS = 'cont')
GROUP BY DISTRICT, servicedate
) As sub1
GROUP BY DISTRICT, monthname, month, Completed
示例数据
例如,我目前看到
区1 - 2 - 6月 1区 - 5区 - 6月
我想看看
1区 - 7月 - 6月
答案 0 :(得分:2)
如果我正确地读了你,你只想看DISTRTICT,SUM(COMPLETED)。如果是这种情况,你应该
SELECT DISTRTICT, SUM(COMPLETED) FROM...
并在最后
GROUP BY DISTRICT
修改强>
根据您的更新请求,我建议您执行以下操作:
SELECT DISTRTICT, SUM(COMPLETED), MONTHNAME FROM...
并在最后
GROUP BY DISTRICT, MONTHNAME
答案 1 :(得分:0)
我没有仔细检查过,但看起来你的问题是你已经“完成”,这是你最后在GROUP BY中的聚合。
将最后一行更改为如下所示,看看是否有效。
GROUP BY DISTRICT, monthname, month