SQL 根据名称分组

SQL Grouping based on Name

我是运行在SQLServer 2008 R2上的以下声明:

DECLARE @Customer nvarchar(100);
SET     @Customer = 'Test';

SELECT  
    ProblemCategorization as 'Category'
    , COUNT(ProblemCategorization) as 'Count'
FROM 
    ININ_ISupport_S_Incident_Search2 (NULL) i  
WHERE 
    IncidentType IN ('Customer Support','Managed Services')
    AND i.Organization = @Customer
    AND i.IsResolved = 0
    AND i.Active = 1
    AND i.StateType = 'Open'
GROUP BY 
    i.ProblemCategorization

结果如下

Client Team 1
Client Team_IC Business Manager 3
Client Team_Interaction Attendant   1
Client Team_Interaction Client .NET 3
Client Team_Session Manager 1

我想把每一个的计数加起来,然后将其分组为一个结果集

Client Count

试试这个。

DECLARE @Customer NVARCHAR(100);

SET @Customer = 'Test';

SELECT CASE
         WHEN i.ProblemCategorization LIKE '%client%' THEN 'client'
       END                          AS 'Category',
       Count(ProblemCategorization) AS 'client Count'
FROM   Inin_isupport_s_incident_search2 (NULL) i
WHERE  IncidentType IN ( 'Customer Support', 'Managed Services' )
       AND i.Organization = @Customer
       AND i.IsResolved = 0
       AND i.Active = 1
       AND i.StateType = 'Open'
GROUP  BY CASE
            WHEN i.ProblemCategorization LIKE '%client%' THEN 'client'
          END 

您调查过 ROLLUP 了吗?

试一试:

DECLARE @Customer nvarchar(100);
SET     @Customer = 'Test';
SELECT  ProblemCategorization as 'Category'
    , COUNT(ProblemCategorization) as 'Count'
FROM ININ_ISupport_S_Incident_Search2 (NULL) i  
WHERE IncidentType IN ('Customer Support','Managed Services')
AND     i.Organization = @Customer
AND     i.IsResolved = 0
AND     i.Active = 1
AND     i.StateType = 'Open'
Group By i.ProblemCategorization with rollup