4

很抱歉之前可能会搞砸这个问题......它仍然让我感到困惑。

基本上,我有一个表来跟踪一组报告的“审批者”。还跟踪报告的“创建”时间和“提交”时间。

我希望标记任何报告的日期/时间重叠。

示例(一个表格,“报告”):

"Created"                 "Submitted"                "ApproverID"
4/20/2012 5:01:32 AM          4/20/2012 5:30:32 AM          10
4/20/2012 5:08:32 AM          4/20/2012 5:45:32 AM          10
4/20/2012 5:01:32 AM          4/19/2012 5:38:16 PM          15 

...我需要返回以下内容(同一批准人重叠日期/时间)

 "Created"                 "Submitted"                "ApproverID"    
4/20/2012 5:01:32 AM          4/20/2012 5:30:32 AM          10            
4/20/2012 5:08:32 AM          4/20/2012 5:45:32 AM          10 

因此审批者(本例中为 10 个)有一个与这些报告重叠的窗口。看起来像是一个带有 BETWEEN 子句的 SELECT ......但我有点难过。

我有一些简单的东西,如下所示:

SELECT Created, LastModified, ApprovalGroupID, count(*) 
FROM   shifthandover.uniquereports
WHERE ApprovalGroupID between Created and LastModified

帮助赞赏!

4

3 回答 3

3

根据您的问题,我了解到在创建第一个(或更早)报告之后但在提交之前的报告之前创建第二个(或后续)报告时会发生“重叠” - 即批准人正在处理多个报告一次。那是对的吗?

您可以尝试将报告表连接到自身 - 例如:

SELECT * FROM reports r1 
JOIN reports r2 
ON r1.approvalId=r2.approvalId 
AND r2.created>r1.created 
AND r2.created<r1.submitted
于 2012-05-15T22:16:05.360 回答
1
SELECT   Created, LastModified, ApprovalGroupID, count(*) --whatever those are
FROM     uniquereports AS r1
JOIN     uniquereports AS r2.ApproverID = r1.ApproverID AND r2 ON r2.id = r1.id -- the unique id of the table, or use whatever combination to get a unique representation, which is important.
WHERE    r2.Created >= r1.Created AND r2.Created <= r1.LastModified OR
         r2.LastModified >= r1.Created AND r2.LastModified <= r1.LastModified OR
         r2.Created <= r1.Created AND r2.LastModified >= r1.LastModified OR
         r2.Created >= r1.Created AND r2.LastModified <= r1.LastModified             
GROUP BY r1.ApproverID

The last OR clause is not useful here since we have already considered the case of r2.Created being greater than r1.Created in first OR clause, considering technically a record can not be modified before its created. Still added there to ensure all cases.

于 2012-05-16T00:27:11.310 回答
1

这提供了批准者 ID 和该批准者的重叠数。

select approver_id, count(*)/2 from (
  select t1.approver_id 
  from reports t1 
  join reports t2 on 
     t2.approver_id = t1.approver_id 
     AND t1.id <> t2.id 
  where
     NOT((t1.submitted < t2.created) 
     OR (t1.created > t2.submitted))
  ) t2 group by approver_id;

count(*)/2 在那里,所以你不会重复计算 A 重叠 B 和 B 重叠 A 的排列。另外,我假设你有一个 record_id 或其他主键来防止记录本身计数。

于 2012-05-16T14:12:47.763 回答