Мне нужно рассчитать диапазон дат для нескольких диапазонов дат одним и тем же ключом.
Прямо сейчас я могу рассчитать разницу между первой входящей датой и последней исходящей датой. Однако у меня есть несколько входящих и исходящих дат, и мне необходимо рассчитать разницу между каждым входящим и последующей датой выхода.
Вот то, что у меня сейчас есть.
SELECT DATEDIFF(SECOND, ql1.TimeInterval, ql2.TimeInterval) / 86400.0 - 2 * DATEDIFF(WEEK, ql1.TimeInterval, ql2.TimeInterval) AS Days_Hours
FROM F_DMQueueLoad ql1
LEFT OUTER JOIN F_DMQueueLoad ql2 ON ql1.Workflow_key = ql2.Workflow_key
AND ql1.Incoming = 1
AND ql2.Outgoing = 1
AND ql1.DMOperation_key = ql2.DMOperation_key
AND ql1.VMAE_F_Subject_key = ql2.VMAE_F_Subject_key
AND ql1.VMAE_wfAuditor_key = ql2.VMAE_wfAuditor_key
AND ql1.VMAE_MEM_MasterPolicyNumber_key = ql2.VMAE_MEM_MasterPolicyNumber_key
AND ql1.VMAE_MEM_InsuredID_key = ql2.VMAE_MEM_InsuredID_key
AND ql1.VMAE_wfBusinessName_key = ql2.VMAE_wfBusinessName_key
AND ql1.VMAE_MEM_MTMDate_key = ql2.VMAE_MEM_MTMDate_key
INNER JOIN D_DMQueueOp q ON ql1.DMOperation_key = q.DMOperation_key
INNER JOIN D_DMDataField_MEM_MasterPolicyNumber mpn ON ql1.VMAE_MEM_MasterPolicyNumber_key = mpn.MEM_MasterPolicyNumber_key
WHERE (q.QueueName LIKE '%MEM_%'
OR q.QueueName LIKE 'Inbox%')
AND (ql2.Outgoing = 1)
AND (ql1.Incoming = 1)
AND ((ql1.Workflow_key + ql1.DMOperation_key + ql1.VMAE_F_Subject_key + CAST(ql1.TimeInterval AS bigint)) IN (SELECT Workflow_key + DMOperation_key + VMAE_F_Subject_key + CAST(MIN(TimeInterval) AS bigint) AS Expr1
FROM F_DMQueueLoad ql3
WHERE (Incoming = 1)
AND (ql1.DMOperation_key = DMOperation_key)
AND (ql1.VMAE_F_Subject_key = VMAE_F_Subject_key)
AND (ql1.VMAE_wfAuditor_key = VMAE_wfAuditor_key)
AND (ql1.VMAE_MEM_MasterPolicyNumber_key = VMAE_MEM_MasterPolicyNumber_key)
AND (ql1.VMAE_MEM_InsuredID_key = VMAE_MEM_InsuredID_key)
AND (ql1.VMAE_wfBusinessName_key = VMAE_wfBusinessName_key)
GROUP BY Workflow_key,
DMOperation_key,
VMAE_F_Subject_key))
AND ((ql2.Workflow_key + ql2.DMOperation_key + ql2.VMAE_F_Subject_key + CAST(ql2.TimeInterval AS bigint)) IN (SELECT Workflow_key + DMOperation_key + VMAE_F_Subject_key + CAST(MAX(TimeInterval) AS bigint) AS Expr1
FROM F_DMQueueLoad ql3
WHERE (Outgoing = 1)
AND (ql1.DMOperation_key = DMOperation_key)
AND (ql1.VMAE_F_Subject_key = VMAE_F_Subject_key)
AND (ql1.VMAE_wfAuditor_key = VMAE_wfAuditor_key)
AND (ql1.VMAE_MEM_MasterPolicyNumber_key = VMAE_MEM_MasterPolicyNumber_key)
AND (ql1.VMAE_MEM_InsuredID_key = VMAE_MEM_InsuredID_key)
AND (ql1.VMAE_wfBusinessName_key = VMAE_wfBusinessName_key)
GROUP BY Workflow_key,
DMOperation_key,
VMAE_F_Subject_key))
AND q.QueueName LIKE '%Representative_ServiceUnit%'
AND mpn.MEM_MasterPolicyNumber = '46572'
AND ql1.workflow_key = '2804566'
GROUP BY ql1.Incoming,
ql2.Outgoing,
ql1.TimeInterval,
ql2.TimeInterval,
ql1.Workflow_key
ORDER BY Days_Hours DESC;
Вот пример моих данных. Входящие и исходящие обозначены 1 в соответствующем столбце.
MPN |Workflow_key |Incoming |Outgoing |TimeInterval
---------|--------------|----------|------------|-----------------------
46572 |2804566 |1 |0 |2019-05-10 09:00:00.000
46572 |2804566 |0 |0 |2019-05-10 09:00:00.000
46572 |2804566 |0 |0 |2019-05-10 09:00:00.000
46572 |2804566 |0 |1 |2019-05-14 13:30:00.000
46572 |2804566 |1 |0 |2019-05-21 13:15:00.000
46572 |2804566 |0 |0 |2019-05-21 13:15:00.000
46572 |2804566 |0 |0 |2019-05-21 13:15:00.000
46572 |2804566 |0 |1 |2019-05-22 13:45:00.000