我遇到以下sql select查詢的問題。這些列不是按組命令聚合的。sql查詢中的列未分組
SELECT
Dept.Name AS DeptName, COUNT (T.Id) AS TotalServiceNumber,
(Case when SS.Status <> 'Resolved' then COUNT (T.Id) end) AS UnresolvedNumber,
(Case when T.FixTime < '120' then COUNT(T.FixTime) end) AS ResolvedLessThanTwoHoursNumber,
(Case when T.FixTime > '120' then COUNT(T.FixTime) end) AS ResolvedMoreThanTwoHoursNumber,
FROM
dbo.Tickets AS T,
dbo.ServiceStatuses AS SS,
dbo.ComputerDesks AS Desk,
dbo.Personnels AS Person,
dbo.Departments AS Dept
WHERE
SS.Id = T.ServiceStatusId
AND T.ComputerDeskId = Desk.Id
AND Desk.PersonnelId = Person.Id
AND Person.DepartmentId = Dept.Id
GROUP BY
Dept.Name, SS.Status, T.FixTime
我得到以下結果:
DeptName | TotalServiceNr | UnresolvedNumber | LessThanTwo | MoreThanTwo
DeptA | 8 | NULL | 8 | NULL
DeptB | 1 | 1 | NULL | 1
DeptC | 4 | NULL | NULL | 4
DeptA | 38 | NULL | NULL | 38
DeptB | 55 | NULL | 55 | NULL
DeptC | 7 | NULL | 7 | NULL
...
預期結果:
DeptName | TotalServiceNr | UnresolvedNumber | LessThanTwo | MoreThanTwo
DeptA | 46 | NULL | 8 | 38
DeptB | 56 | 1 | 55 | NULL
DeptC | 11 | NULL | 7 | 4
我需要改變,以獲得期望的結果?
您是否將所有ServiceStatus行分組爲一個結果,而不管它們是否已解決? – thudbutt