我想在SQL中使用类似于COUNTIFS函数的函数

时间:2017-02-17 00:13:50

标签: sql sql-server excel

我想在SQL

中使用类似于COUNTIFS函数的函数

这个查询应该给我一张符合SLA的门票数量(门票已经在不到10分钟的时间内被接受,并且根据优先级在特定时间内被解决)

我的查询中的SLA部分给了我一个错误

(Msg 130, Level 15, State 1, Line 94
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.)

我尝试了上述查询,但收到了错误

Declare @Top  int = null             --<<  Sets top of Hier Try 12
Declare @Nest varchar(25) ='|-----'  --<<  Optional: Added for readability

;with cteHB (Seq,Path,PROBLEM_TYPE_ID,PARENT_ID,Lvl,PROBLEM_TYPE_NAME) as (
    Select  Seq  = cast(1000+Row_Number() over (Order by PROBLEM_TYPE_NAME) as varchar(500))
           ,Path = cast(PROBLEM_TYPE_ID as varchar(500))
           ,PROBLEM_TYPE_ID
           ,PARENT_ID
           ,Lvl=1
           ,PROBLEM_TYPE_NAME 
     From   problem_type 
     Where  IsNull(@Top,-1) = case when @Top is null then isnull(PARENT_ID,-1) else PROBLEM_TYPE_ID end
     Union  All
     Select Seq  = cast(concat(cteHB.Seq,'.',1000+Row_Number() over (Order by cteCD.PROBLEM_TYPE_NAME)) as varchar(500))
           ,Path = cast(concat(cteHB.Path,'.',cteCD.PROBLEM_TYPE_ID) as varchar(500))
           ,cteCD.PROBLEM_TYPE_ID
           ,cteCD.PARENT_ID,cteHB.Lvl+1
           ,cteCD.PROBLEM_TYPE_NAME 
     From   problem_type cteCD 
     Join   cteHB on cteCD.PARENT_ID = cteHB.PROBLEM_TYPE_ID)
    ,cteR1 as (Select Seq,PROBLEM_TYPE_ID,R1=Row_Number() over (Order By Seq) From cteHB)
    ,cteR2 as (Select A.Seq,A.PROBLEM_TYPE_ID,R2=Max(B.R1) From cteR1 A Join cteR1 B on (B.Seq like A.Seq+'%') Group By A.Seq,A.PROBLEM_TYPE_ID )
    ,cteFinalHier as (
        Select B.R1  
              ,C.R2
              ,A.PROBLEM_TYPE_ID
              ,A.PARENT_ID
              ,A.Lvl
              ,PROBLEM_TYPE_NAME = Replicate(@Nest,A.Lvl-1) + A.PROBLEM_TYPE_NAME
              ,A.Seq                                      -- < Included for Illustration
              ,A.Path                                     -- < Included for Illustration
         From cteHB A
         Join cteR1 B on A.PROBLEM_TYPE_ID=B.PROBLEM_TYPE_ID
         Join cteR2 C on A.PROBLEM_TYPE_ID=C.PROBLEM_TYPE_ID
    )
Select A.Job_ticket_id

      ,[Problem_Type_Name(Parent)]=C.PROBLEM_TYPE_NAME
      ,[Problem_Type_Name(Child)] =B.PROBLEM_TYPE_NAME
      ,HISTORY_ENTRY.ENTRY_DATE
      ,case when HISTORY_ENTRY.ENTRY_TEXT like max('%Assigned to %%NOC%Level 2%') and HISTORY_ENTRY.ENTRY_TEXT not like max('%Assigned to %%NOC%Level 1%')
      or HISTORY_ENTRY.ENTRY_TEXT like max('%Escalated to %%NOC%Level 2%%') and HISTORY_ENTRY.ENTRY_TEXT not like max('%Escalated to %%NOC%Level 1%')
      or HISTORY_ENTRY.ENTRY_TEXT like max('%Status changed from % to hold%%')
      or HISTORY_ENTRY.ENTRY_TEXT like max('%Status changed from %Hold to %%') 
      or HISTORY_ENTRY.ENTRY_TEXT like max('%Status changed from % to Resolved%%')
      or HISTORY_ENTRY.ENTRY_TEXT like max('%Status changed from %Resolved to %%')
      or HISTORY_ENTRY.ENTRY_TEXT like max('%Status changed from % to Closed%%')
      or HISTORY_ENTRY.ENTRY_TEXT like max('%Ticket Type changed%')
      or HISTORY_ENTRY.ENTRY_TEXT like max('%Reopen%') 
      then HISTORY_ENTRY.ENTRY_TEXT end as 'History Entry' 


,DATEDIFF(MINUTE, (select max(ENTRY_DATE) from history_entry where a.job_Ticket_ID = HISTORY_ENTRY.job_Ticket_ID and HISTORY_ENTRY.entry_text like '%Status changed from % to hold%%'), 
                                (select max(ENTRY_DATE) from history_entry where a.job_Ticket_ID = HISTORY_ENTRY.job_Ticket_ID and HISTORY_ENTRY.entry_text like '%Status changed from %Hold to %%')) as 'hold time'

,datediff(minute,(select max(ENTRY_DATE) from history_entry where a.job_Ticket_ID = HISTORY_ENTRY.job_Ticket_ID and HISTORY_ENTRY.ENTRY_TEXT like ('%Escalated to %%NOC%Level 2%%') and HISTORY_ENTRY.ENTRY_TEXT not like ('%Escalated to %%NOC%Level 1%')),
 (select max(ENTRY_DATE) from history_entry where a.job_Ticket_ID = HISTORY_ENTRY.job_Ticket_ID and HISTORY_ENTRY.ENTRY_TEXT like ('%Assigned to %%NOC%Level 2%') and HISTORY_ENTRY.ENTRY_TEXT not like ('%Assigned to %%NOC%Level 1%'))) as 'Time to Accept SLA' ----> (this is time difference captured from the history details of ticket when it was first Escalated to level 2 till the time it was accepted by technician in Level 2)

 ,datediff(minute,(select max(ENTRY_DATE) from history_entry where a.job_Ticket_ID = HISTORY_ENTRY.job_Ticket_ID and HISTORY_ENTRY.ENTRY_TEXT like ('%Escalated to %%NOC%Level 2%%') and HISTORY_ENTRY.ENTRY_TEXT not like ('%Escalated to %%NOC%Level 1%')),
 (select max(ENTRY_DATE) from history_entry where a.job_Ticket_ID = HISTORY_ENTRY.job_Ticket_ID and HISTORY_ENTRY.entry_text like '%Status changed from % to resolved%%')) as 'Escalated to Resolved time' ----> (this is time difference captured from the history details of ticket when it was first Escalated to level 2 till the time its status changed to resolved)

 ,datediff(MINUTE,(select max(ENTRY_DATE) from history_entry where a.job_Ticket_ID = HISTORY_ENTRY.job_Ticket_ID and HISTORY_ENTRY.ENTRY_TEXT like ('%Escalated to %%NOC%Level 2%%') and HISTORY_ENTRY.ENTRY_TEXT not like ('%Escalated to %%NOC%Level 1%')),
 (select max(ENTRY_DATE) from history_entry where a.job_Ticket_ID = HISTORY_ENTRY.job_Ticket_ID and HISTORY_ENTRY.entry_text like '%Status changed from % to closed%%')) as 'Escalated to Closed time' ----> (this is time difference captured from the history details of ticket when it was first Escalated to level 2 till the time its status changed to closed)


      ,A.Report_Date
      ,A.LAST_UPDATED
       ,STATUS_TYPE.STATUS_TYPE_NAME as 'Ticket Status'
      ,a.STATUS_TYPE_ID AS 'status Id'
      ,A.Close_Date
      ,A.TECH_GROUP_ID
       ,isnull(tech.lAST_NAME,'') + ' ' +isnull(Tech.FIRST_NAME,'') [Assigned Tech]
      ,TECH_GROUP_LEVEL.LEVEL_NUMBER

      ,DATEDIFF(MINUTE,a.LAST_UPDATED,getdate()) as 'greater than 72 hours'
      ,TECH_GROUP.NAME
      ,PRIORITY_TYPE_NAME

----------------------------SLA----------------------------
 ,CAST ((sum(case WHEN datediff(minute,(select max(ENTRY_DATE) from history_entry where a.job_Ticket_ID = HISTORY_ENTRY.job_Ticket_ID and HISTORY_ENTRY.ENTRY_TEXT like ('%Escalated to %%NOC%Level 2%%') and HISTORY_ENTRY.ENTRY_TEXT not like ('%Escalated to %%NOC%Level 1%')),
 (select max(ENTRY_DATE) from history_entry where a.job_Ticket_ID = HISTORY_ENTRY.job_Ticket_ID and HISTORY_ENTRY.ENTRY_TEXT like ('%Assigned to %%NOC%Level 2%') and HISTORY_ENTRY.ENTRY_TEXT not like ('%Assigned to %%NOC%Level 1%'))) <= 10         and 
 datediff(MINUTE,(select max(ENTRY_DATE) from history_entry where a.job_Ticket_ID = HISTORY_ENTRY.job_Ticket_ID and HISTORY_ENTRY.ENTRY_TEXT like ('%Escalated to %%NOC%Level 2%%') and HISTORY_ENTRY.ENTRY_TEXT not like ('%Escalated to %%NOC%Level 1%')),
 (select max(ENTRY_DATE) from history_entry where a.job_Ticket_ID = HISTORY_ENTRY.job_Ticket_ID and HISTORY_ENTRY.entry_text like '%Status changed from % to closed%%')) <= case PRIORITY_TYPE_NAME
                                   WHEN 'low' then 960
                                   WHEN 'medium' then 480
                                   WHEN 'high' then 120
                                   WHEN 'Urgent' then 60
                                   end
  then 1
  else 0
  end)*100.0)/COUNT(*) as money) as Percent_Compliant
------------------------------JOINS--------------------------------------------------                                                                                   
 From  JOB_TICKET A
 Join  cteFinalHier B on A.PROBLEM_TYPE_ID=B.PROBLEM_TYPE_ID

 INNER JOIN [SWHD01].[dbo].[PRIORITY_TYPE] ON A.[PRIORITY_TYPE_ID] = [PRIORITY_TYPE].[PRIORITY_TYPE_ID]

 INNER JOIN [SWHD01].[dbo].[STATUS_TYPE] ON A.[STATUS_TYPE_ID] = [STATUS_TYPE].[STATUS_TYPE_ID]

  inner join [SWHD01].[dbo].TECH_GROUP_LEVEL on A.TECH_GROUP_LEVEL_ID=TECH_GROUP_LEVEL.ID

 join TECH_GROUP on TECH_GROUP.ID= TECH_GROUP_LEVEL.tech_group_id



 LEFT JOIN TECH on Tech.CLIENT_ID = A.ASSIGNED_TECH_ID
join HISTORY_ENTRY on a.JOB_TICKET_ID=HISTORY_ENTRY.JOB_TICKET_ID


 Cross Apply (Select Top 1 * from cteFinalHier Where B.R1 between R1 and R2 and Lvl=1) C

 -------------Tickets for the Last 6 months---------------------------------------------------

 where datediff(day, A.REPORT_DATE, getdate()) <= 30
and TECH_GROUP.NAME like '%NOC%'
 and LEVEL_NUMBER ='2'




 Group By C.PROBLEM_TYPE_NAME,a.JOB_TICKET_ID,B.PROBLEM_TYPE_NAME,B.PROBLEM_TYPE_ID, a.REPORT_DATE,a.CLOSE_DATE,SWHD01.dbo.PRIORITY_TYPE.PRIORITY_TYPE_NAME,a.FIRST_RESPONSE_DATE,B.R1,a.LAST_UPDATED,a.STATUS_TYPE_ID,a.TECH_GROUP_ID,TECH_GROUP_LEVEL.LEVEL_NUMBER,TECH_GROUP.NAME,a.ASSIGNED_TECH_ID,HISTORY_ENTRY.ENTRY_DATE,HISTORY_ENTRY.ENTRY_TEXT,HISTORY_ENTRY.TECH_ID,TECH.LAST_NAME,TECH.FIRST_NAME,STATUS_TYPE.STATUS_TYPE_NAME
 Order By job_ticket_id desc

我希望代码能够100%返回已满足SLA的票证,以下是我的完整代码:

collection.toString()

1 个答案:

答案 0 :(得分:0)

((总和(案例WHEN datediff(分钟,(从history_entry中选择最大值(ENTRY_DATE),其中a.job_Ticket_ID = HISTORY_ENTRY.job_Ticket_ID和HISTORY_ENTRY.ENTRY_TEXT之类的('%已升级为%% NOC%等级2 %%')和HISTORY_ENTRY.ENTRY_TEXT不喜欢('%已升级到%% NOC%等级1%'),  (从history_entry中选择max(ENTRY_DATE),其中a.job_Ticket_ID = HISTORY_ENTRY.job_Ticket_ID和HISTORY_ENTRY.ENTRY_TEXT喜欢('%Assigned to %% NOC%Level 2%')和HISTORY_ENTRY.ENTRY_TEXT不喜欢('%Assigned to %% NOC%等级1%')))&lt; = 10和  datediff(MINUTE,(从history_entry中选择max(ENTRY_DATE),其中a.job_Ticket_ID = HISTORY_ENTRY.job_Ticket_ID和HISTORY_ENTRY.ENTRY_TEXT之类('%Escalated to %% NOC%Level 2 %%')和HISTORY_ENTRY.ENTRY_TEXT不相似('%Escalated至%% NOC%等级1%')),  (从history_entry中选择max(ENTRY_DATE),其中a.job_Ticket_ID = HISTORY_ENTRY.job_Ticket_ID和HISTORY_ENTRY.entry_text,例如'%Status已从%更改为已关闭%%'))&lt; = case PRIORITY_TYPE_NAME                                    当'低'然后960                                    当'中等'然后480                                    当'高'然后120                                    当'紧急'然后60                                    结束   然后1   否则0
  结束) 100.0)/ COUNT(*)&lt; - 我相信这应该是这样的 (SELECT COUNT( )FROM ..... WHERE ...)作为total_count,因为SUM(COUNT)无效,它应该是SUM(每行的1个值)而不是SUM (每行COUNT(每行))     作为钱)作为Percent_Compliant