根据条件筛选

时间:2019-07-24 15:49:19

标签: sql sql-server

我有一个查询,我在哪里获取某些票证的数据。现在我需要放置一个where条件以在timediffsecs <> 0处获取数据。我无法找到放置conditon的正确位置。    下面是我的代码。请帮助

 WITH CTE
AS
(
    SELECT cr.ref_num as 'TicketNumber',
            isnull(requestor.last_name,'') + ', ' + isnull(requestor.first_name,'') as 'Requestor'
        ,ISNULL(cnt.last_name, '') + ', ' + ISNULL(cnt.first_name,'') as 'Created By'
                ,isnull(aeu.last_name,'') + ', ' + isnull(aeu.first_name,'') as 'Affected End User',
                         isnull(logagent.last_name,'') + ', ' + isnull(logagent.first_name,'') as 'Reported By'
         ,dbo.ConvertUnixTime (cr.open_date) as 'Open Date'
         ,dbo.ConvertUnixTime (cr.last_mod_dt) as 'Last Modified Date'
         ,dbo.ConvertUnixTime (cr.resolve_date) as 'Resolve Date'
         ,dbo.ConvertUnixTime (cr.close_date) as 'Close Date'
        ,dbo.ConvertUnixTime (act.time_stamp) as 'systime',
        cr.summary as 'Summary'
         ,convert(varchar(max),cr.[description]) as 'Description'
        ,act.[action_desc] as 'System Description'
        ,acttype.sym as 'Activity Type'
        ,act.time_spent as 'Time Spent',

        ROW_NUMBER() OVER(PARTITION BY cr.ref_num ORDER BY dbo.ConvertUnixTime (act.time_stamp)) RN
 --   ROW_NUMBER() OVER(ORDER BY dbo.ConvertUnixTime (act.time_stamp) ) RN
    -- ROW_NUMBER generated based on ORDER BY Time DESC
    -- You can also add case_id or any other column to generate
    --ROW_NUMBER so that the time differences can be calculate 
    --based on other dimension.
from  call_req cr with (nolock) 

LEFT OUTER JOIN ca_contact requestor with (nolock) on cr.requested_by = requestor.Contact_uuid
LEFT OUTER JOIN ca_contact aeu with (nolock) on cr.customer = aeu.Contact_uuid
LEFT OUTER JOIN ca_contact logagent with (nolock) on cr.log_agent = logagent.Contact_uuid
INNER JOIN act_log act with (nolock) on cr.persid = act.call_req_id
INNER JOIN ca_contact cnt with (nolock) on act.analyst = cnt.contact_uuid
INNER JOIN act_type acttype with (nolock) on act.[type] = acttype.code
where  cr.ref_num in ('23035883',
'23038276') 
)
SELECT *,
CASE 
    WHEN A.RN = 3 THEN
  DATEDIFF(
            SECOND,
            (SELECT systime  FROM CTE WHERE RN = 2 AND TicketNumber= A.TicketNumber),
            (SELECT systime  FROM CTE WHERE RN = 3 AND TicketNumber= A.TicketNumber)



            -- By setting RN = 3 and 2, I am calculating 
            -- Time differences between ROW 3 and 2
            -- You can set it to any Row number as per your requirement
        )
    ELSE 0
END as timediffsecs 
FROM CTE A

0 个答案:

没有答案