如何计算多个日期的日期差?

时间:2019-06-25 16:01:09

标签: sql-server-2012 ssrs-2012

我需要使用相同的键来计算多个日期范围的日期范围。

现在,我能够计算出第一个入局日期和最后一个出局日期之间的差额。但是,我有多个传入和传出日期,因此需要计算每个传入与后续传出日期之间的差异。

这是我目前拥有的。

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

0 个答案:

没有答案