如何排除0的COUNT个结果?

时间:2015-01-02 22:23:41

标签: sql sql-server

我有一个查询,可以获得分配给团队的所有门票的数量:

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”票回来的结果?

4 个答案:

答案 0 :(得分:4)

您可以对每个查询使用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的答案,因为您的查询效率非常低。

答案 1 :(得分:1)

而不是在一堆查询之间使用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 

答案 2 :(得分:1)

编写此查询的另一种方法是避免所有这些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

答案 3 :(得分:1)

您可以使用公用表表达式而不是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