Ето 3 различни варианта. Нямам SQL 2000, с който да тествам, но това трябва да работи. Всяка опция може да работи по различен начин предвид размера на таблицата и структурата на данните.
ЗАБЕЛЕЖКА: за всеки, който вижда този отговор, ако използвате SQL Server 2008 R2 или по-нова версия, тогава COUNT DISTINCT работи дори когато използвате ROLLUP. Може да работи с SQL 2005, но нямам екземпляр на SQL 05, с който да тествам.
Използване на подзаявка:
SELECT t.annee, t.mois, t.siteId, t.id, t.cnt, t.downtime
, (
SELECT count(distinct i2.department)
FROM incident i2
WHERE (i2.annee = t.annee
or i2.annee is null and t.annee is null and t.isgroup_annee = 0
or t.isgroup_annee = 1)
and (i2.mois = t.mois
or i2.mois is null and t.mois is null and t.isgroup_mois = 0
or t.isgroup_mois = 1)
and (i2.siteId = t.siteId
or i2.siteId is null and t.siteId is null and t.isgroup_siteId = 0
or t.isgroup_siteId = 1)
and (i2.id = t.id
OR i2.id is null and t.id is null and t.isgroup_id = 0
OR t.isgroup_id = 1)
) as departmentCnt
FROM (
SELECT i.annee, i.mois, i.siteId, i.id, count(i.id) as cnt, sum(i.downtime) as downtime
, grouping(i.annee) AS isgroup_annee
, grouping(i.mois) AS isgroup_mois
, grouping(i.siteId) AS isgroup_siteId
, grouping(i.id) AS isgroup_Id
FROM incident i
GROUP BY i.annee, i.mois, i.siteId, i.id
WITH ROLLUP
) t
;
Използване на присъединяване:
SELECT t.annee, t.mois, t.siteId, t.cnt, t.downtime
, count(distinct i2.department) as departmentCnt
FROM (
SELECT i.annee, i.mois, i.siteId, i.id, count(i.id) as cnt, sum(i.downtime) as downtime
, grouping(i.annee) AS isgroup_annee
, grouping(i.mois) AS isgroup_mois
, grouping(i.siteId) AS isgroup_siteId
, grouping(i.id) AS isgroup_Id
FROM incident i
GROUP BY i.annee, i.mois, i.siteId, i.id
WITH ROLLUP
) t
LEFT JOIN incident i2
ON (i2.annee = t.annee
or i2.annee is null and t.annee is null and t.isgroup_annee = 0
or t.isgroup_annee = 1)
and (i2.mois = t.mois
or i2.mois is null and t.mois is null and t.isgroup_mois = 0
or t.isgroup_mois = 1)
and (i2.siteId = t.siteId
or i2.siteId is null and t.siteId is null and t.isgroup_siteId = 0
or t.isgroup_siteId = 1)
and (i2.id = t.id
OR i2.id is null and t.id is null and t.isgroup_id = 0
OR t.isgroup_id = 1)
GROUP BY t.annee, t.mois, t.siteId, t.id, t.cnt, t.downtime, t.isgroup_annee, t.isgroup_mois, t.isgroup_siteId, t.isgroup_Id
;
използване на UNIONs за ръчно изпълнение на ROLLUP:
SELECT i.annee, i.mois, i.siteId, i.id, count(i.id) as cnt, sum(i.downtime) as downtime, count(distinct i.department) as departmentCnt
FROM incident i
GROUP BY i.annee, i.mois, i.siteId, i.id
UNION ALL
SELECT i.annee, i.mois, i.siteId, null as id, count(i.id) as cnt, sum(i.downtime) as downtime, count(distinct i.department) as departmentCnt
FROM incident i
GROUP BY i.annee, i.mois, i.siteId
UNION ALL
SELECT i.annee, i.mois, null as siteId, null as id, count(i.id) as cnt, sum(i.downtime) as downtime, count(distinct i.department) as departmentCnt
FROM incident i
GROUP BY i.annee, i.mois
UNION ALL
SELECT i.annee, null as mois, null as siteId, null as id, count(i.id) as cnt, sum(i.downtime) as downtime, count(distinct i.department) as departmentCnt
FROM incident i
GROUP BY i.annee
UNION ALL
SELECT null as annee, null as mois, null as siteId, null as id, count(i.id) as cnt, sum(i.downtime) as downtime, count(distinct i.department) as departmentCnt
FROM incident i
;
person
BateTech
schedule
16.05.2014
i.id
във вашияgroup by
? Изглежда, че не сте го направили, тъй като се опитвате да извършите агрегат върху него. - person BateTech   schedule 16.05.2014