需要输出基于基于TimeDifference的计数或权重

时间:2011-08-18 01:54:16

标签: sql sql-server sql-server-2005 tsql sql-server-2008

TID   StartTime                   Uid   WId
1     2011-06-06 09:30:00.000     10    1.5
2     2011-06-06 09:40:00.000     10    3.5
3     2011-06-06 09:50:00.000     10    1.0
4     2011-06-06 10:45:00.000     10    2.5
5     2011-06-06 10:50:00.000     10    1.5
6     2011-06-06 10:55:00.000     10    0
7     2011-06-06 11:30:00.000     10    0
8     2011-06-06 11:35:00.000     10    0
9     2011-06-06 11:40:00.000     10    0
10    2011-06-06 11:43:00.000     10    0
11    2011-06-06 11:20:00.000     11    7.0
12    2011-06-06 11:30:00.000     11    1.0

我有表TblTask​​中的数据,我需要根据每个Starttime的时间差和另一个时间差为1小时或接近1小时的启动时间来编写查询来计算(sum(Wid)或Count(TID))由UID分组。

例如取第一个开始时间(2011-06-06 09:30:00.000);具有< = 1hr的最近开始时间是(2011-06-06 10:30:00.000)(如果SUM(Wid)> = 5.0或行数> = 5)。这里的权重是(1.5 + 3.5 + 1.0 = 6.0)  前3行TID(1,2,3)作为权重> = 5我们需要显示记录和  TID(3,4,5)这里的重量是(1.0 + 2.5 + 1.5 = 5.0)  对于TID'd(4,5,6,7,8,9,10)这里计数(TID是7)所以我想显示4到10的所有记录

对于TID(5,6,7,8,9,10)作为Count(TID是> 5)我想显示5到10的所有记录

对于TID(6,7,8,9,10)作为Count(TID是= 5)我希望显示从6到10的所有记录

对于TID 11,WID> 5我想显示记录11

对于TID(11,12),WID> 5再次,我想显示记录11和12

所需的输出将是:

TID    StartTime                 Uid  
1     2011-06-06 09:30:00.000    10    
2     2011-06-06 09:40:00.000    10     
3     2011-06-06 09:50:00.000    10   
3     2011-06-06 09:50:00.000    10    
4     2011-06-06 10:45:00.000    10    
5     2011-06-06 10:50:00.000    10    
4     2011-06-06 10:45:00.000    10    
5     2011-06-06 10:50:00.000    10    
6     2011-06-06 10:55:00.000    10    
7     2011-06-06 11:30:00.000    10    
8     2011-06-06 11:35:00.000    10    
9     2011-06-06 11:40:00.000    10    
10    2011-06-06 11:43:00.000    10    
5     2011-06-06 10:50:00.000    10    
6     2011-06-06 10:55:00.000    10    
7     2011-06-06 11:30:00.000    10    
8     2011-06-06 11:35:00.000    10    
9     2011-06-06 11:40:00.000    10    
10    2011-06-06 11:43:00.000    10    
6     2011-06-06 10:55:00.000    10    
7     2011-06-06 11:30:00.000    10    
8     2011-06-06 11:35:00.000    10    
9     2011-06-06 11:40:00.000    10    
10    2011-06-06 11:43:00.000    10    
11    2011-06-06 11:20:00.000    11 
11    2011-06-06 11:20:00.000    11    
12    2011-06-06 11:30:00.000    11   

1 个答案:

答案 0 :(得分:0)

备选方案I:不会捕获单行11,因为它将尝试从每个起始点选择最长的有效序列。

SELECT C.*
FROM (
    SELECT
        A.UID,
        A.TID AS TID1,
        MAX(B.TID) AS TID2
    FROM TblTask A
    INNER JOIN TblTask B
        ON B.UID = A.UID
        AND B.StartTime BETWEEN A.StartTime AND DATEADD(hour, 1, A.StartTime)
    GROUP BY A.TID, A.UID
    HAVING SUM(B.WId) >= 5 OR COUNT(*) >= 5
) Ranges
INNER JOIN TblTask C
    ON C.UID = Ranges.UID
    AND C.TID BETWEEN Ranges.TID1 AND Ranges.TID2

备选方案II:将同时报告(4..9)和(4..10),因为这些都符合标准。

SELECT D.*
FROM (
    SELECT
        A.Uid,
        A.TID AS TID1,
        B.TID AS TID2
    FROM TblTask A
    INNER JOIN TblTask B
        ON B.Uid = A.Uid
        AND B.StartTime BETWEEN A.StartTime AND DATEADD(hour, 1, A.StartTime)
    INNER JOIN TblTask C
        ON C.UID = A.UID
        AND C.TID BETWEEN A.TID AND B.TID
    GROUP BY A.TID, B.TID, A.Uid
    HAVING SUM(C.WId) >= 5  OR COUNT(*) >= 5
) Ranges
INNER JOIN TblTask D
    ON D.Uid = Ranges.Uid
    AND D.TID BETWEEN Ranges.TID1 AND Ranges.TID2;

在任何一种方法中,您都可以添加Ranges表中的列来区分连接的子序列。

备选方案I的结果:

TID   StartTime                 Uid   WId
  1   2011-06-06 09:30:00.000    10   1.5
  2   2011-06-06 09:40:00.000    10   3.5
  3   2011-06-06 09:50:00.000    10   1.0
  3   2011-06-06 09:50:00.000    10   1.0
  4   2011-06-06 10:45:00.000    10   2.5
  5   2011-06-06 10:50:00.000    10   1.5
  4   2011-06-06 10:45:00.000    10   2.5
  5   2011-06-06 10:50:00.000    10   1.5
  6   2011-06-06 10:55:00.000    10   0.0
  7   2011-06-06 11:30:00.000    10   0.0
  8   2011-06-06 11:35:00.000    10   0.0
  9   2011-06-06 11:40:00.000    10   0.0
 10   2011-06-06 11:43:00.000    10   0.0
  5   2011-06-06 10:50:00.000    10   1.5
  6   2011-06-06 10:55:00.000    10   0.0
  7   2011-06-06 11:30:00.000    10   0.0
  8   2011-06-06 11:35:00.000    10   0.0
  9   2011-06-06 11:40:00.000    10   0.0
 10   2011-06-06 11:43:00.000    10   0.0
  6   2011-06-06 10:55:00.000    10   0.0
  7   2011-06-06 11:30:00.000    10   0.0
  8   2011-06-06 11:35:00.000    10   0.0
  9   2011-06-06 11:40:00.000    10   0.0
 10   2011-06-06 11:43:00.000    10   0.0
 11   2011-06-06 11:20:00.000    11   7.0
 12   2011-06-06 11:30:00.000    11   1.0

备选方案II的结果:

TID   StartTime                 Uid   WId
  1   2011-06-06 09:30:00.000    10   1.5
  2   2011-06-06 09:40:00.000    10   3.5
  1   2011-06-06 09:30:00.000    10   1.5
  2   2011-06-06 09:40:00.000    10   3.5
  3   2011-06-06 09:50:00.000    10   1.0
  3   2011-06-06 09:50:00.000    10   1.0
  4   2011-06-06 10:45:00.000    10   2.5
  5   2011-06-06 10:50:00.000    10   1.5
  4   2011-06-06 10:45:00.000    10   2.5
  5   2011-06-06 10:50:00.000    10   1.5
  6   2011-06-06 10:55:00.000    10   0.0
  7   2011-06-06 11:30:00.000    10   0.0
  8   2011-06-06 11:35:00.000    10   0.0
  4   2011-06-06 10:45:00.000    10   2.5
  5   2011-06-06 10:50:00.000    10   1.5
  6   2011-06-06 10:55:00.000    10   0.0
  7   2011-06-06 11:30:00.000    10   0.0
  8   2011-06-06 11:35:00.000    10   0.0
  9   2011-06-06 11:40:00.000    10   0.0
  4   2011-06-06 10:45:00.000    10   2.5
  5   2011-06-06 10:50:00.000    10   1.5
  6   2011-06-06 10:55:00.000    10   0.0
  7   2011-06-06 11:30:00.000    10   0.0
  8   2011-06-06 11:35:00.000    10   0.0
  9   2011-06-06 11:40:00.000    10   0.0
 10   2011-06-06 11:43:00.000    10   0.0
  5   2011-06-06 10:50:00.000    10   1.5
  6   2011-06-06 10:55:00.000    10   0.0
  7   2011-06-06 11:30:00.000    10   0.0
  8   2011-06-06 11:35:00.000    10   0.0
  9   2011-06-06 11:40:00.000    10   0.0
  5   2011-06-06 10:50:00.000    10   1.5
  6   2011-06-06 10:55:00.000    10   0.0
  7   2011-06-06 11:30:00.000    10   0.0
  8   2011-06-06 11:35:00.000    10   0.0
  9   2011-06-06 11:40:00.000    10   0.0
 10   2011-06-06 11:43:00.000    10   0.0
  6   2011-06-06 10:55:00.000    10   0.0
  7   2011-06-06 11:30:00.000    10   0.0
  8   2011-06-06 11:35:00.000    10   0.0
  9   2011-06-06 11:40:00.000    10   0.0
 10   2011-06-06 11:43:00.000    10   0.0
 11   2011-06-06 11:20:00.000    11   7.0
 11   2011-06-06 11:20:00.000    11   7.0
 12   2011-06-06 11:30:00.000    11   1.0