如何让一个列只有在sql中没有过滤器(where和having)的特定值

时间:2017-05-01 23:56:54

标签: sql sql-server

Expected output

我有一个名为String_Value的列,它有很多垃圾值和一些重要的值。

如果我只筛选出警告,重要等重要值。我将只获得那些与过滤器匹配的string_value的job_ticket_ids,这也将为我提供不同的工作ID。

但我需要的是string_value列应该只包含没有任何过滤器的重要值,对于没有那些重要值的作业ID,可以返回null。工作ID也应该是截然不同的。

我附上了我的预期输出供您参考。如果您有任何想法,请指导我。

select  distinct 
    Job_ticket_id,
    CASE WHEN (case when [Escalated to Resolved time] is null then (ISNULL([Escalated to Resolved time],0) + ISNULL([Escalated to Closed time],0)- ISNULL([hold time],0)) else (ISNULL([Escalated to Closed time],0) - ISNULL([hold time],0)) end) <0THEN 0 ELSE (case when [Escalated to Resolved time] is null then (ISNULL([Escalated to Resolved time],0) + ISNULL([Escalated to Closed time],0)- ISNULL([hold time],0)) else (ISNULL([Escalated to Closed time],0) - ISNULL([hold time],0)) end)  END AS JobTime,
    CASE WHEN ISNULL(cat.[Time to Accept SLA],0) <0 THEN 0 ELSE ISNULL(cat.[Time to Accept SLA],0) END AS [Time to Accept SLA],
    cat.Report_Date,
    cat.[Problem_Type_Name(Parent)],
    cat.[Problem_Type_Name(Child)],
    cat.[Assigned Tech],
    cat.PRIORITY_TYPE_NAME, cat.Close_Date,
    cat.NAME, cat.[Ticket Status],
    isnull(max(cat.[Escalated to Closed time]), 0) as 'Escalated to Closed time',
    isnull(max(cat.[Escalated to Resolved time]), 0) as 'Escalated to Resolved time',
    isnull(max(cat.[hold time]), 0) as 'Hold Time',
    cat.String_value as 'String_value',
    cat.PROBLEM_TYPE_ID
from 
    TEMP_TICKET_STATE_Category cat
group by 
    JOB_TICKET_ID, String_value, [Problem_Type_Name(Parent)], 
    JOB_TICKET_ID, [Problem_Type_Name(Child)], PROBLEM_TYPE_ID, 
    REPORT_DATE, CLOSE_DATE, PRIORITY_TYPE_NAME, LAST_UPDATED,
    TECH_GROUP_ID, NAME, ENTRY_DATE, 
    [Escalated to Closed time], [Escalated to Resolved time],
    [hold time], [Time to Accept SLA], [Assigned Tech], [Ticket Status]

1 个答案:

答案 0 :(得分:0)

您可以尝试以下代码:

select  distinct Job_ticket_id,
CASE WHEN (case when [Escalated to Resolved time] is null then (ISNULL([Escalated to Resolved time],0) + ISNULL([Escalated to Closed time],0)- ISNULL([hold time],0)) else (ISNULL([Escalated to Closed time],0) - ISNULL([hold time],0)) end) <0THEN 0 ELSE (case when [Escalated to Resolved time] is null then (ISNULL([Escalated to Resolved time],0) + ISNULL([Escalated to Closed time],0)- ISNULL([hold time],0)) else (ISNULL([Escalated to Closed time],0) - ISNULL([hold time],0)) end)  END AS JobTime 
,CASE  WHEN ISNULL(cat.[Time to Accept SLA],0) <0 THEN 0 ELSE ISNULL(cat.[Time to Accept SLA],0) END AS [Time to Accept SLA]
, cat.Report_Date
, cat.[Problem_Type_Name(Parent)]
, cat.[Problem_Type_Name(Child)]
, cat.[Assigned Tech]
, cat.PRIORITY_TYPE_NAME
,cat.Close_Date,cat.NAME
,cat.[Ticket Status]
,isnull(max(cat.[Escalated to Closed time]),0) as 'Escalated to Closed time'
,isnull(max(cat.[Escalated to Resolved time]),0) as 'Escalated to Resolved time'
,isnull(max(cat.[hold time]),0) as 'Hold Time'
,case when cat.String_value in ('warning' ,'critical') then cat.String_value else null end  as 'String_value'
,cat.PROBLEM_TYPE_ID
from TEMP_TICKET_STATE_Category cat
 Group By JOB_TICKET_ID,String_value,[Problem_Type_Name(Parent)],JOB_TICKET_ID,[Problem_Type_Name(Child)],PROBLEM_TYPE_ID, REPORT_DATE,CLOSE_DATE,PRIORITY_TYPE_NAME,LAST_UPDATED,TECH_GROUP_ID,NAME,ENTRY_DATE,[Escalated to Closed time],[Escalated to Resolved time],[hold time],[Time to Accept SLA],[Assigned Tech],[Ticket Status]