Sql server SQL Server:与组中的下一个X进行比较

Sql server SQL Server:与组中的下一个X进行比较,sql-server,sql-server-2012,Sql Server,Sql Server 2012,我知道我可以用滞后和超前来比较上一行/下一行,但在我所做的比较中;我需要能够偶尔跳过行,但仍然需要确认它们 具体说明:我试图在通话环境中跟踪咨询、冷暖转接。每个呼叫都有一个唯一的键,每个阶段都有一个递增的员工编号。它还为通话的每个部分提供开始和结束时间。 consult被定义为在前一个非consult段结束之前开始和结束的调用。 热传输是指前一段的结束在前一段的开始之后,并在前一段的结束之后结束。 冷传输是在前一段结束后开始的段 示例数据: topcallid e_created

我知道我可以用滞后和超前来比较上一行/下一行,但在我所做的比较中;我需要能够偶尔跳过行,但仍然需要确认它们

具体说明:我试图在通话环境中跟踪咨询、冷暖转接。每个呼叫都有一个唯一的键,每个阶段都有一个递增的员工编号。它还为通话的每个部分提供开始和结束时间。 consult被定义为在前一个非consult段结束之前开始和结束的调用。 热传输是指前一段的结束在前一段的开始之后,并在前一段的结束之后结束。 冷传输是在前一段结束后开始的段

示例数据:

topcallid e_created e_terminated empnum EG995GIFM 16:22:40.933 16:29:51.010 1 EG995GIFM 16:25:59.827 16:27:49.027 2 EG995GIFM 16:30:07.453 16:37:44.500 3 EG995GIFM 16:38:01.677 16:59:30.777 4 EG995GIFM 16:59:46.737 17:16:48.397 5 EG995GIFM 17:04:51.243 17:29:21.620 6 当前代码,它给出了将每个阶段与以下所有阶段进行比较的每个组合:

SELECT ta.topcallid,
       CASE 
            WHEN ta.e_created < Trans.e_created AND ta.e_terminated > Trans.e_terminated THEN 'Consult (Stage ' + CAST(TA.EmpNum AS VARCHAR(3)) + ' To ' + CAST(Trans.EmpNum AS VARCHAR(3)) + ')'
            WHEN ta.e_terminated < Trans.e_created THEN 'Cold Transfer (Stage ' + CAST(TA.EmpNum AS VARCHAR(3)) + ' To ' + CAST(Trans.EmpNum AS VARCHAR(3)) + ')'
            WHEN ta.e_terminated > Trans.e_created THEN 'Warm Transfer (Stage ' + CAST(TA.EmpNum AS VARCHAR(3)) + ' To ' + CAST(Trans.EmpNum AS VARCHAR(3)) + ')'
       END TransStatus
FROM   [TransferTypeAnalysis] TA
       JOIN [TransferTypeAnalysis] Trans
            ON  TA.topcallid = Trans.topcallid
                AND ta.empnum < Trans.empnum
ORDER BY
       TA.topcallid,
       ta.empnum

好吧,我想我在这件事上支持你


在您的连接条件中,change和ta.empnum试试这个。它基本上只做一次延迟来获取咨询,然后丢弃它们,然后再次进行延迟来获取传输,然后合并表

SELECT *
,CASE 
    WHEN lag(e_terminated) OVER (
            PARTITION BY topcallID ORDER BY e_created
            ) IS NULL
        THEN NULL
    WHEN lag(e_terminated) OVER (
            PARTITION BY topcallID ORDER BY e_created
            ) > e_terminated
        THEN 'Consult ' + cast(lag(EmpNum) OVER (
                    PARTITION BY topcallID ORDER BY e_created
                    ) AS VARCHAR) + ' to ' + cast(empnum AS VARCHAR)
    WHEN lag(e_terminated) OVER (
            PARTITION BY topcallID ORDER BY e_created
            ) < e_created
        THEN 'Cold Transfer ' + cast(lag(EmpNum) OVER (
                    PARTITION BY topcallID ORDER BY e_created
                    ) AS VARCHAR) + ' to ' + cast(empnum AS VARCHAR)
    ELSE 'Warm Transfer ' + cast(lag(EmpNum) OVER (
                PARTITION BY topcallID ORDER BY e_created
                ) AS VARCHAR) + ' to ' + cast(empnum AS VARCHAR)
    END AS StatusTemp
INTO #temp1
FROM MyTable

SELECT a.topcallID
,coalesce(b.StatusTemp2, a.StatusTemp) AS TransStatus
FROM #temp1 a
LEFT JOIN (
SELECT *
    ,CASE 
        WHEN lag(e_terminated) OVER (
                PARTITION BY topcallID ORDER BY e_created
                ) IS NULL
            THEN NULL
        WHEN lag(e_terminated) OVER (
                PARTITION BY topcallID ORDER BY e_created
                ) > e_terminated
            THEN 'Consult ' + cast(lag(EmpNum) OVER (
                        PARTITION BY topcallID     ORDER BY e_created
                        ) AS VARCHAR) + ' to ' + cast(empnum AS VARCHAR)
        WHEN lag(e_terminated) OVER (
                PARTITION BY topcallID ORDER BY e_created
                ) < e_created
            THEN 'Cold Transfer ' + cast(lag(EmpNum) OVER (
                        PARTITION BY topcallID ORDER BY e_created
                        ) AS VARCHAR) + ' to ' + cast(empnum AS VARCHAR)
        ELSE 'Warm Transfer ' + cast(lag(EmpNum) OVER (
                    PARTITION BY topcallID ORDER BY e_created
                    ) AS VARCHAR) + ' to ' + cast(empnum AS VARCHAR)
        END AS StatusTemp2
FROM #temp1
WHERE StatusTemp NOT LIKE 'Consult%'
    OR StatusTemp IS NULL
) b
ON a.empNum = b.empnum
    AND a.topcallID = b.topCallID
where coalesce(b.StatusTemp2, a.StatusTemp) is not null

您似乎需要一个子查询来与上一个终止的调用进行比较。此逻辑假定每个呼叫id的终止时间是唯一的

select 
    t3.topcallid, 
    case when t3.e_created < t4.e_created and t3.e_terminated > t4.e_terminated then 'Consult (Stage ' +  cast(t3.EmpNum as varchar(3)) + ' To ' + cast(t4.EmpNum as varchar(3)) + ')'
        when t3.e_terminated < t4.e_created then 'Cold Transfer (Stage ' +  cast(t3.EmpNum as varchar(3)) + ' To ' +  cast(t4.EmpNum as varchar(3)) + ')'
        when t3.e_terminated > t4.e_created then 'Warm Transfer (Stage ' +  cast(t3.EmpNum as varchar(3)) + ' To ' +  cast(t4.EmpNum as varchar(3)) + ')'
        end TransStatus
from
    [TransferTypeAnalysis] t3
    INNER JOIN
    (
        select 
            t1.topcallid, t1.empnum, t1.e_created, t1.e_terminated, max(t2.e_terminated) previous_term_dt
        FROM 
            [TransferTypeAnalysis] t1
            left join [TransferTypeAnalysis] t2
            on t1.topcallid=t2.topcallid and t1.empnum>t2.empnum
        GROUP BY t1.topcallid, t1.empnum, t1.e_created, t1.e_terminated
    ) as t4 on t3.topcallid = t4.topcallid and t3.e_terminated = t4.previous_term_dt
您还可以在无界前置项和1前置项之间使用MAX with OVER and ROWS,以获取每行任何前置项的最大终止时间

一旦每个条目都有了“最大上一次终止时间”,就可以使用与当前代码中相同的方式进行自连接

样本数据

质疑


+1将在下一个连接,这是LEAD将要做的,但是,如果您查看所需的输出,它有1->3,因为报告了2,但不是传输链的一部分啊!对不起,我没听清楚。今晚下班回家后我会尽力帮忙的为了澄清,如果2是正确的单词,您希望跳过序列中的2,但仍将其视为咨询。这是因为empnum2终止后empnum 1终止?不知道为什么,但是冷转移箱行被代码格式截断了,我没有更改大小写逻辑,所以您可以使用原始的codeStackExchange喜欢认为您在答案中使用HTML,所以它可以作为未知的HTML命令删除整个部分
SELECT *
,CASE 
    WHEN lag(e_terminated) OVER (
            PARTITION BY topcallID ORDER BY e_created
            ) IS NULL
        THEN NULL
    WHEN lag(e_terminated) OVER (
            PARTITION BY topcallID ORDER BY e_created
            ) > e_terminated
        THEN 'Consult ' + cast(lag(EmpNum) OVER (
                    PARTITION BY topcallID ORDER BY e_created
                    ) AS VARCHAR) + ' to ' + cast(empnum AS VARCHAR)
    WHEN lag(e_terminated) OVER (
            PARTITION BY topcallID ORDER BY e_created
            ) < e_created
        THEN 'Cold Transfer ' + cast(lag(EmpNum) OVER (
                    PARTITION BY topcallID ORDER BY e_created
                    ) AS VARCHAR) + ' to ' + cast(empnum AS VARCHAR)
    ELSE 'Warm Transfer ' + cast(lag(EmpNum) OVER (
                PARTITION BY topcallID ORDER BY e_created
                ) AS VARCHAR) + ' to ' + cast(empnum AS VARCHAR)
    END AS StatusTemp
INTO #temp1
FROM MyTable

SELECT a.topcallID
,coalesce(b.StatusTemp2, a.StatusTemp) AS TransStatus
FROM #temp1 a
LEFT JOIN (
SELECT *
    ,CASE 
        WHEN lag(e_terminated) OVER (
                PARTITION BY topcallID ORDER BY e_created
                ) IS NULL
            THEN NULL
        WHEN lag(e_terminated) OVER (
                PARTITION BY topcallID ORDER BY e_created
                ) > e_terminated
            THEN 'Consult ' + cast(lag(EmpNum) OVER (
                        PARTITION BY topcallID     ORDER BY e_created
                        ) AS VARCHAR) + ' to ' + cast(empnum AS VARCHAR)
        WHEN lag(e_terminated) OVER (
                PARTITION BY topcallID ORDER BY e_created
                ) < e_created
            THEN 'Cold Transfer ' + cast(lag(EmpNum) OVER (
                        PARTITION BY topcallID ORDER BY e_created
                        ) AS VARCHAR) + ' to ' + cast(empnum AS VARCHAR)
        ELSE 'Warm Transfer ' + cast(lag(EmpNum) OVER (
                    PARTITION BY topcallID ORDER BY e_created
                    ) AS VARCHAR) + ' to ' + cast(empnum AS VARCHAR)
        END AS StatusTemp2
FROM #temp1
WHERE StatusTemp NOT LIKE 'Consult%'
    OR StatusTemp IS NULL
) b
ON a.empNum = b.empnum
    AND a.topcallID = b.topCallID
where coalesce(b.StatusTemp2, a.StatusTemp) is not null
select 
    t3.topcallid, 
    case when t3.e_created < t4.e_created and t3.e_terminated > t4.e_terminated then 'Consult (Stage ' +  cast(t3.EmpNum as varchar(3)) + ' To ' + cast(t4.EmpNum as varchar(3)) + ')'
        when t3.e_terminated < t4.e_created then 'Cold Transfer (Stage ' +  cast(t3.EmpNum as varchar(3)) + ' To ' +  cast(t4.EmpNum as varchar(3)) + ')'
        when t3.e_terminated > t4.e_created then 'Warm Transfer (Stage ' +  cast(t3.EmpNum as varchar(3)) + ' To ' +  cast(t4.EmpNum as varchar(3)) + ')'
        end TransStatus
from
    [TransferTypeAnalysis] t3
    INNER JOIN
    (
        select 
            t1.topcallid, t1.empnum, t1.e_created, t1.e_terminated, max(t2.e_terminated) previous_term_dt
        FROM 
            [TransferTypeAnalysis] t1
            left join [TransferTypeAnalysis] t2
            on t1.topcallid=t2.topcallid and t1.empnum>t2.empnum
        GROUP BY t1.topcallid, t1.empnum, t1.e_created, t1.e_terminated
    ) as t4 on t3.topcallid = t4.topcallid and t3.e_terminated = t4.previous_term_dt
DECLARE @TransferTypeAnalysis TABLE ( topcallid VARCHAR(20),e_created TIME,e_terminated TIME,empnum INT)

insert into @TransferTypeAnalysis
VALUES('EG995GIFM',   '16:22:40.933', '16:29:51.010', 1),
('EG995GIFM',   '16:25:59.827', '16:27:49.027', 2),
('EG995GIFM',   '16:30:07.453', '16:37:44.500', 3),
('EG995GIFM',   '16:38:01.677',  '16:59:30.777', 4),
('EG995GIFM',  '16:59:46.737', '17:16:48.397', 5),
('EG995GIFM',   '17:04:51.243', '17:29:21.620', 6);
SELECT ta.topcallid,
    CASE WHEN TA_Prev.e_created<TA.e_created and TA_Prev.e_terminated>TA.e_terminated THEN 'Consult'
        WHEN TA_Prev.e_terminated<TA.e_created THEN 'Cold Transfer'
        WHEN TA_Prev.e_terminated>TA.e_created THEN 'Warm Transfer'
        END  + ' (Stage '+  cast(TA_Prev.EmpNum AS VARCHAR(3)) + ' To ' + cast(TA.EmpNum AS VARCHAR(3)) + ')'TransStatus
FROM
(
SELECT *,MAX(e_terminated)OVER(PARTITION BY topcallid ORDER BY empnum ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) as max_term
FROM @TransferTypeAnalysis
)TA
INNER JOIN @TransferTypeAnalysis TA_Prev
ON TA.topcallid = TA_Prev.topcallid AND TA.max_term = TA_Prev.e_terminated AND TA.empnum > TA_Prev.empnum