How to find percentage of sum with multiple like conditions

Tamayo, Ashley 121 Reputation points
2020-11-06T20:59:25.627+00:00

SELECT DISTINCT CASE WHEN IncidentTypeCode like ('[][5-9][0-9]') OR (IncidentTypeCode like ('[5-9][0-9][a-z][0-9][0-9]') OR IncidentTypeCode like ('[5-9][0-9][a-z][0-9][0-9][a-z]'))THEN 'Fire' WHEN IncidentTypeCode like ('[][0-3][0-9]') OR (IncidentTypeCode like ('[0-3][0-9][a-z][0-9][0-9]') OR IncidentTypeCode like ('[0-3][0-9][a-z][0-9][0-9][a-z]') OR IncidentTypeCode in ('_NT','_ET','_ma')) THEN 'Medical' END AS callType ,IncidentNumber ,IncidentTypeCode ,IncidentDate FROM MV_Incident WHERE IncidentDate >='11/6/2020'--@IncidentDate --AND IncidentDate <= @IncidentDate2 --AND ReportNumberAgencyID IN (@Agency) AND FirstUnitDispatchedTime IS NOT NULL AND IncidentTypeCode NOT IN ('_test','_SD','BP') ORDER BY IncidentDate I'm trying to calculate what percentage of incident numbers were 'medical' and what percentage were 'fire'. Unfortunately I do not have a specific column that categorizes the call types this way except for a case statement. I can't seem to find the right syntax to use because I have several conditions. When I run the query above I get a result like this: ![38132-results.jpg][1] [1]: /api/attachments/38132-results.jpg?platform=QnA EDIT 11/20/2020 After further investigation I realized that the query below in the answer was not equaling 100% and it was giving different calculations than when the math was preformed manually. I collaborated with another resource within our organization and discovered that the following query met the need requested above. WITH CTE AS ( SELECT COUNT (DISTINCT CASE WHEN IncidentTypeCode like '[][0-3][0-9]' OR (IncidentTypeCode like '[0-3][0-9][a-z][0-9][0-9]' or (IncidentTypeCode like '[0-3][0-9][a-z][0-9][0-9][a-z]' or (IncidentTypeCode in ('_NT','_ET','ma')))) then IncidentNumber END) as MedicalCount ,COUNT (DISTINCT CASE WHEN IncidentTypeCode like '[][5-9][0-9]' OR (IncidentTypeCode like '[5-9][0-9][a-z][0-9][0-9]' or (IncidentTypeCode like '[5-9][0-9][a-z][0-9][0-9][a-z]')) then IncidentNumber END) as FireCount ,COUNT(DISTINCT CASE WHEN IncidentTypeCode NOT IN ('_test','_SD','_BP') then IncidentNumber END) AS CountofAllCalls FROM MV_Incident WHERE IncidentDate >=@IncidentDate AND IncidentDate <= @IncidentDate2 AND ReportNumberAgencyID IN (@Agency) AND FirstUnitDispatchedTime IS NOT NULL AND IncidentTypeCode NOT IN ('_test','_SD','_BP') ) SELECT * ,sum(medicalcount) * 100.00/ sum(sum(countofallcalls)) over () as medpercnt ,sum(FireCount) * 100.00/ sum(sum(countofallcalls)) over () as firepercnt FROM CTE group by medicalcount, firecount,CountofAllCalls

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,670 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,623 questions
0 comments No comments
{count} votes

Accepted answer
  1. MelissaMa-MSFT 24,196 Reputation points
    2020-11-09T08:34:52.267+00:00

    Hi anonymous user,

    Please also refer below query and check whether it is helpful to you:

    ; WITH CTE AS (  
          SELECT   
             CASE  
              WHEN IncidentTypeCode like ('[_][5-9][0-9]') OR (IncidentTypeCode like ('[5-9][0-9][a-z][0-9][0-9]') OR IncidentTypeCode like ('[5-9][0-9][a-z][0-9][0-9][a-z]'))THEN  'Fire'   
              WHEN IncidentTypeCode like ('[_][0-3][0-9]') OR (IncidentTypeCode like ('[0-3][0-9][a-z][0-9][0-9]') OR IncidentTypeCode like ('[0-3][0-9][a-z][0-9][0-9][a-z]') OR IncidentTypeCode in ('_NT','_ET','_ma'))  THEN 'Medical'      
              END AS callType    
            ,IncidentNumber  
            ,IncidentTypeCode  
            ,IncidentDate    
          FROM  MV_Incident   
          WHERE  
            IncidentDate >='11/6/2020'--@IncidentDate  
            --AND IncidentDate <= @IncidentDate2  
            --AND ReportNumberAgencyID IN (@Agency)  
            AND FirstUnitDispatchedTime IS NOT NULL  
            AND IncidentTypeCode NOT IN ('_test','_SD','_BP')  
     )  
      select a.*,b.percentage    
       FROM   CTE a  
      inner join   
     (SELECT callType,  
      convert(decimal(5, 2),Count(callType)* 100 / (Select Count(*) From CTE)*0.01) as percentage   
     FROM   CTE  
      group by callType) b  
      on a.callType=b.callType  
    

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    Hot issues October--Users always get connection timeout problem when using multi subnet AG via listener. Especially after failover to another subnet


2 additional answers

Sort by: Most helpful
  1. Guoxiong 8,206 Reputation points
    2020-11-06T21:06:55.38+00:00

    Put your query to the CTE and then you can calculate the percentages of different call types.

    0 comments No comments

  2. Erland Sommarskog 110.3K Reputation points
    2020-11-06T22:49:24.35+00:00

    Elaborating on Guoxiong's suggestion:

    ; WITH CTE AS (
         SELECT 
            CASE
             WHEN IncidentTypeCode like ('[_][5-9][0-9]') OR (IncidentTypeCode like ('[5-9][0-9][a-z][0-9][0-9]') OR IncidentTypeCode like ('[5-9][0-9][a-z][0-9][0-9][a-z]'))THEN  'Fire' 
             WHEN IncidentTypeCode like ('[_][0-3][0-9]') OR (IncidentTypeCode like ('[0-3][0-9][a-z][0-9][0-9]') OR IncidentTypeCode like ('[0-3][0-9][a-z][0-9][0-9][a-z]') OR IncidentTypeCode in ('_NT','_ET','_ma'))  THEN 'Medical'    
             END AS callType  
           ,IncidentNumber
           ,IncidentTypeCode
           ,IncidentDate  
         FROM  MV_Incident 
         WHERE
           IncidentDate >='11/6/2020'--@IncidentDate
           --AND IncidentDate <= @IncidentDate2
           --AND ReportNumberAgencyID IN (@Agency)
           AND FirstUnitDispatchedTime IS NOT NULL
           AND IncidentTypeCode NOT IN ('_test','_SD','_BP')
    )
    SELECT callType, IncidentNumber, IncidentTypeCode, IncidentDate,
           convert(decimal(5, 2), 1E2*COUNT(*) OVER (PARTITION BY CallType) / COUNT(*))
    FROM   CTE
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.