我遇到以下sql select查询问题。这些列不是由group by命令聚合的。
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
我需要更改以获得预期结果?
答案 0 :(得分:1)
尝试此查询:
SELECT
Dept.Name AS DeptName, COUNT (T.Id) AS TotalServiceNumber,
sum(Case when SS.Status <> 'Resolved' then 1 else 0 end) AS UnresolvedNumber,
sum(Case when T.FixTime <= '120' then 1 else 0 end) AS ResolvedLessThanTwoHoursNumber,
sum(Case when T.FixTime > '120' then 1 else 0 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
答案 1 :(得分:0)
试试这个
SELECT TotalServiceNumber, SUM(UnresolvedNumber), SUM(ResolvedLessThanTwoHoursNumber), SUM(ResolvedMoreThanTwoHoursNumber)
FROM (
SELECT
Dept.Name AS DeptName, COUNT (T.Id) AS TotalServiceNumber,
(Case when SS.Status <> 'Resolved' then COUNT (C.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
) GROUPED
GROUP BY
TotalServiceNumber
答案 2 :(得分:0)
SELECT
Dept.Name AS DeptName
, COUNT (T.Id) AS TotalServiceNumber
,COUNT(Case when SS.Status <> 'Resolved' then 1 ELSE NULL end) AS UnresolvedNumber
,COUNT(Case when T.FixTime < '120' then 1 ELSE NULL end) AS ResolvedLessThanTwoHoursNumber
,COUNT(Case when T.FixTime > '120' then 1 ELSE NULL end) AS ResolvedMoreThanTwoHoursNumber
FROM
dbo.Tickets AS T INNER JOIN dbo.ServiceStatuses AS SS
ON SS.Id = T.ServiceStatusId
INNER JOIN dbo.ComputerDesks AS Desk
ON T.ComputerDeskId = Desk.Id
INNER JOIN dbo.Personnels AS Person
ON Desk.PersonnelId = Person.Id
INNER JOIN dbo.Departments AS Dept
Person.DepartmentId = Dept.Id
GROUP BY Dept.Name
还为联接使用ON
语法。