子查询帮助

时间:2009-06-21 17:21:18

标签: sql

您好我有下面的查询,我想要区分不同的区域以及每个属性的总和是多少但我不记得如何进行子查询!!!

任何人都可以帮忙吗

SELECT DISTRICT, PROPREF

FROM TBL_PROPERTY 

WHERE  (CONTRACT = 'ma2') AND 
        (LASTSERVICEDATE <= DATEADD(HH,23,CONVERT(DATETIME,'30/05/2009', 103))) AND 
        (SERVICESTATUS = 'SERVICED')) AS Sub1


GROUP BY DISTRICT, OFFICE, PROPREF

3 个答案:

答案 0 :(得分:1)

我想你想要这个:

select DISTRICT, count(PROPREF)
from TBL_PROPERTY
where CONTRACT = 'ma2'
    and LASTSERVICEDATE <= DATEADD(HH,23,CONVERT(DATETIME,'30/05/2009', 103))
    and SERVICESTATUS = 'SERVICED'
group by DISTRICT

如果您还希望办公室将其分解,您可以将OFFICE添加到select和group by子句中。

答案 1 :(得分:1)

SELECT COUNT(*) AS PROPERTY_COUNT ...

这就是你要找的东西吗?

答案 2 :(得分:0)

对不起,如果有人回应了!!也忘了上任,我想我最终还是把它拿走了。

SELECT DISTINCT(DISTRICT), SUM(services) AS TOTALSERVICES

FROM 

(SELECT DISTRICT, COUNT(PROPREF) AS Services

FROM TBL_PROPERTY 

WHERE  (CONTRACT = 'ma2') AND 
        (LASTSERVICEDATE <= DATEADD(HH,23,CONVERT(DATETIME,'30/05/2009', 103))) AND 
        (SERVICESTATUS = 'SERVICED')
GROUP BY DISTRICT, propref

) as sub1

GROUP BY DISTRICT, services