如何排除 COUNT 个结果为 0?
How can I exclude COUNT results which are 0?
我有一个查询,用于获取分配给团队的所有门票的计数:
SELECT 'Application Developers' AS team, COUNT(Assignee) AS tickets
from mytable WHERE status = 'Open' AND Assignee like '%Application__bDevelopers%'
UNION ALL
SELECT 'Desktop Support' AS team, COUNT(Assignee) AS tickets
from mytable WHERE status = 'Open' AND Assignee like '%Desktop__bSupport%'
UNION ALL
SELECT 'Network Management' AS team, COUNT(Assignee) AS tickets
FROM mytable WHERE status = 'Open' AND Assignee LIKE '%Network__bManagement%'
UNION ALL
SELECT 'Security' AS team, COUNT(Assignee) AS tickets
from mytable WHERE status = 'Open' AND Assignee = '%Security%'
UNION ALL
SELECT 'Telecom' AS team, COUNT(Assignee) AS tickets
from mytable WHERE status = 'Open' AND Assignee = '%Telecom%'
结果是:
team tickets
Application Developers 6
Desktop Support 374
Network Management 0
Security 7
Telecom 0
如何排除带有“0”票的返回结果?
您可以对每个查询使用 HAVING
:
SELECT 'Application Developers' AS team, COUNT(Assignee) AS tickets
from mytable WHERE
status = 'Open'
AND Assignee like '%Application__bDevelopers%'
HAVING COUNT(Assignee) <> 0
我建议@NoDisplayName 的回答,因为你的查询目前效率很低。
而不是在一堆查询之间使用 Union all 像这样更改您的查询。
使用Having clause
过滤群组所在count <> 0
SELECT CASE
WHEN Assignee LIKE '%Application__bDevelopers%' THEN 'Application Developers'
WHEN Assignee LIKE '%Desktop__bSupport%' THEN 'Desktop Support'
WHEN Assignee LIKE '%Network__bManagement%' THEN 'Network Management'
WHEN Assignee LIKE '%Security%' THEN 'Security'
WHEN Assignee LIKE '%Telecom%' THEN 'Telecom'
END AS team,
Count(Assignee) AS tickets
FROM mytable
WHERE status = 'Open'
AND ( Assignee LIKE '%Application__bDevelopers%'
OR Assignee LIKE '%Desktop__bSupport%'
OR Assignee LIKE '%Network__bManagement%'
OR Assignee = '%Security%'
OR Assignee = '%Telecom%' )
GROUP BY CASE
WHEN Assignee LIKE '%Application__bDevelopers%' THEN 'Application Developers'
WHEN Assignee LIKE '%Desktop__bSupport%' THEN 'Desktop Support'
WHEN Assignee LIKE '%Network__bManagement%' THEN 'Network Management'
WHEN Assignee LIKE '%Security%' THEN 'Security'
WHEN Assignee LIKE '%Telecom%' THEN 'Telecom'
END
HAVING Count(Assignee) <> 0
编写此查询的另一种方法是避免所有这些 UNION ALL 查询并执行类似
的操作
SELECT Teams
,Tickets
FROM
(
SELECT NULLIF(COUNT(CASE WHEN Assignee LIKE '%Application__bDevelopers%'
THEN 1 ELSE NULL END),0) AS [Application__bDevelopers]
,NULLIF(COUNT(CASE WHEN Assignee LIKE '%Desktop__bSupport%'
THEN 1 ELSE NULL END),0) AS [Desktop__bSupport]
,NULLIF(COUNT(CASE WHEN Assignee LIKE '%Network__bManagement%'
THEN 1 ELSE NULL END),0) AS [Network__bManagement]
,NULLIF(COUNT(CASE WHEN Assignee LIKE '%Security%'
THEN 1 ELSE NULL END),0) AS [Security]
,NULLIF(COUNT(CASE WHEN Assignee LIKE '%Telecom%'
THEN 1 ELSE NULL END),0) AS [Telecom]
FROM mytable
WHERE [status] = 'Open' ) Q
UNPIVOT (Tickets FOR Teams IN ([Application__bDevelopers], [Desktop__bSupport]
,[Network__bManagement],[Security],[Telecom]))up
您可以使用常用 table 表达式而不是 Having
;WITH CTE AS
(
SELECT 'Application Developers' AS team, COUNT(Assignee) AS tickets
from mytable WHERE status = 'Open' AND Assignee like '%Application__bDevelopers%'
UNION ALL
SELECT 'Desktop Support' AS team, COUNT(Assignee) AS tickets
from mytable WHERE status = 'Open' AND Assignee like '%Desktop__bSupport%'
UNION ALL
SELECT 'Network Management' AS team, COUNT(Assignee) AS tickets
FROM mytable WHERE status = 'Open' AND Assignee LIKE '%Network__bManagement%'
UNION ALL
SELECT 'Security' AS team, COUNT(Assignee) AS tickets
from mytable WHERE status = 'Open' AND Assignee = '%Security%'
UNION ALL
SELECT 'Telecom' AS team, COUNT(Assignee) AS tickets
from mytable WHERE status = 'Open' AND Assignee = '%Telecom%'
)
SELECT
*
FROM CTE
WHERE tickets>0
我有一个查询,用于获取分配给团队的所有门票的计数:
SELECT 'Application Developers' AS team, COUNT(Assignee) AS tickets
from mytable WHERE status = 'Open' AND Assignee like '%Application__bDevelopers%'
UNION ALL
SELECT 'Desktop Support' AS team, COUNT(Assignee) AS tickets
from mytable WHERE status = 'Open' AND Assignee like '%Desktop__bSupport%'
UNION ALL
SELECT 'Network Management' AS team, COUNT(Assignee) AS tickets
FROM mytable WHERE status = 'Open' AND Assignee LIKE '%Network__bManagement%'
UNION ALL
SELECT 'Security' AS team, COUNT(Assignee) AS tickets
from mytable WHERE status = 'Open' AND Assignee = '%Security%'
UNION ALL
SELECT 'Telecom' AS team, COUNT(Assignee) AS tickets
from mytable WHERE status = 'Open' AND Assignee = '%Telecom%'
结果是:
team tickets
Application Developers 6
Desktop Support 374
Network Management 0
Security 7
Telecom 0
如何排除带有“0”票的返回结果?
您可以对每个查询使用 HAVING
:
SELECT 'Application Developers' AS team, COUNT(Assignee) AS tickets
from mytable WHERE
status = 'Open'
AND Assignee like '%Application__bDevelopers%'
HAVING COUNT(Assignee) <> 0
我建议@NoDisplayName 的回答,因为你的查询目前效率很低。
而不是在一堆查询之间使用 Union all 像这样更改您的查询。
使用Having clause
过滤群组所在count <> 0
SELECT CASE
WHEN Assignee LIKE '%Application__bDevelopers%' THEN 'Application Developers'
WHEN Assignee LIKE '%Desktop__bSupport%' THEN 'Desktop Support'
WHEN Assignee LIKE '%Network__bManagement%' THEN 'Network Management'
WHEN Assignee LIKE '%Security%' THEN 'Security'
WHEN Assignee LIKE '%Telecom%' THEN 'Telecom'
END AS team,
Count(Assignee) AS tickets
FROM mytable
WHERE status = 'Open'
AND ( Assignee LIKE '%Application__bDevelopers%'
OR Assignee LIKE '%Desktop__bSupport%'
OR Assignee LIKE '%Network__bManagement%'
OR Assignee = '%Security%'
OR Assignee = '%Telecom%' )
GROUP BY CASE
WHEN Assignee LIKE '%Application__bDevelopers%' THEN 'Application Developers'
WHEN Assignee LIKE '%Desktop__bSupport%' THEN 'Desktop Support'
WHEN Assignee LIKE '%Network__bManagement%' THEN 'Network Management'
WHEN Assignee LIKE '%Security%' THEN 'Security'
WHEN Assignee LIKE '%Telecom%' THEN 'Telecom'
END
HAVING Count(Assignee) <> 0
编写此查询的另一种方法是避免所有这些 UNION ALL 查询并执行类似
的操作SELECT Teams
,Tickets
FROM
(
SELECT NULLIF(COUNT(CASE WHEN Assignee LIKE '%Application__bDevelopers%'
THEN 1 ELSE NULL END),0) AS [Application__bDevelopers]
,NULLIF(COUNT(CASE WHEN Assignee LIKE '%Desktop__bSupport%'
THEN 1 ELSE NULL END),0) AS [Desktop__bSupport]
,NULLIF(COUNT(CASE WHEN Assignee LIKE '%Network__bManagement%'
THEN 1 ELSE NULL END),0) AS [Network__bManagement]
,NULLIF(COUNT(CASE WHEN Assignee LIKE '%Security%'
THEN 1 ELSE NULL END),0) AS [Security]
,NULLIF(COUNT(CASE WHEN Assignee LIKE '%Telecom%'
THEN 1 ELSE NULL END),0) AS [Telecom]
FROM mytable
WHERE [status] = 'Open' ) Q
UNPIVOT (Tickets FOR Teams IN ([Application__bDevelopers], [Desktop__bSupport]
,[Network__bManagement],[Security],[Telecom]))up
您可以使用常用 table 表达式而不是 Having
;WITH CTE AS
(
SELECT 'Application Developers' AS team, COUNT(Assignee) AS tickets
from mytable WHERE status = 'Open' AND Assignee like '%Application__bDevelopers%'
UNION ALL
SELECT 'Desktop Support' AS team, COUNT(Assignee) AS tickets
from mytable WHERE status = 'Open' AND Assignee like '%Desktop__bSupport%'
UNION ALL
SELECT 'Network Management' AS team, COUNT(Assignee) AS tickets
FROM mytable WHERE status = 'Open' AND Assignee LIKE '%Network__bManagement%'
UNION ALL
SELECT 'Security' AS team, COUNT(Assignee) AS tickets
from mytable WHERE status = 'Open' AND Assignee = '%Security%'
UNION ALL
SELECT 'Telecom' AS team, COUNT(Assignee) AS tickets
from mytable WHERE status = 'Open' AND Assignee = '%Telecom%'
)
SELECT
*
FROM CTE
WHERE tickets>0