0
我有一個包含以下內容的表:SQL總和只有獨特價值
plantId (pk)
unitId
maxCapacity
numberOfPlants
geographicalArea
agreementStart
agreementEnd
plantType
表包含以下數據:
1, A1, 10, 1, 3, 2013-01-01, 2013-12-31
2, A2, 10, 1, 3, 2013-01-01, 2013-12-31
3, A1, 10, 1, 3, 2013-03-01, 2013-12-31
我想查詢我的表是這樣的:
SELECT DISTINCT
plantType,
geographicalArea,
sum(maxCapacity) as maxCapacity,
sum(numberOfPlants) as numberOfPlants,
count(unitId) as idCount
FROM
tbl_plant
WHERE
(agreementStart <= '2013-11-28' AND
agreementEnd >= '2013-11-28') AND
plantType <> '0' AND
plantType = '2' AND
geographicalArea = '3'
GROUP BY
plantType, geographicalArea
只要在r中每行返回一個唯一的unitId
,此查詢就可以正常工作esultset。但是,如果第二次返回相同的unitId
(請參見上面的A1
),我不希望sum(maxCapacity)
和sum(numberOfPlants)
第二次包括該和,因爲它已經包含了它。
任何想法,我需要改變這個查詢?
優秀的,謝謝。直接工作。 – mediasurface