根据计数条件对多行进行分组和填充

时间:2016-01-07 09:36:35

标签: tsql sql-server-2005 aggregate

我有一个每10分钟运行一次的脚本,并返回包含过去24小时内事件的表格(以脚本运行时标记)

ID      Name                TimeOfEvent                 EventCategory       TeamColor
1       Verlene Bucy        2015-01-30 09:10:00.000         1                   Blue
2       Geneva Rendon       2015-01-30 09:20:00.000         2                   Blue
3       Juliane Hartwig     2015-01-30 09:25:00.000         3                   Blue
4       Vina Dutton         2015-01-30 12:55:00.000         2                   Red
5       Cristin Lewis       2015-01-30 15:50:00.000         2                   Red
6       Reiko Cushman       2015-01-30 17:10:00.000         1                   Red
7       Mallie Temme        2015-01-30 18:35:00.000         3                   Blue
8       Keshia Seip         2015-01-30 19:55:00.000         2                   Blue
9       Rosalia Maher       2015-01-30 20:35:00.000         3                   Red
10      Keven Gabel         2015-01-30 21:25:00.000         3                   Red

现在我想根据这些条件选择两组名称:

1)从过去24小时内有4条或更多记录的同一EventCategory中选择名称。

2)从同一个EventCategory中选择名称,并在过去1小时内选择具有2个或更多记录的相同TeamColor。

所以我的结果是:

4+per24h: Geneva Rendon, Vina Dutton, Cristin Lewis, Keshia Seip        EventCategory = 2
4+per24h: Juliane Hartwig, Mallie Temme, Rosalia Maher, Keven Gabel     EventCategory = 3
2+per1h:  Rosalia Maher, Keven Gabel                                    EventCategory = 3, TeamColor = Red

对于第一个,我写了这个:

SELECT mt.EventCategory, MAX(mt.[name]), MAX(mt.TimeOfEvent), MAX(mt.TeamColor)
  FROM #mytable mt
GROUP BY mt.EventCategory
HAVING COUNT(mt.EventCategory) >= 4

因为我不关心实际时间,只要它在过去的24小时内(而且一直都是这样),但是我把这些名字排成一行有困难。

第二部分,我不知道该怎么做。因为结果需要同时具有相同的EventCategory和TeamColor,并且还受到一小时限制的限制。

1 个答案:

答案 0 :(得分:1)

这是可能的,但你混合了两个不同的问题。在这里,您会发现它们与<img src="x.y" alt="" />&nbsp;<img src="RTE.jpg" alt="" /><br /><img src="RTE.jpg" alt="" /> <img src="CPE.ztc"> 结合使用:

只需将其粘贴到空查询窗口并执行即可。适应您的需求:

UNION

结果

DECLARE @tbl TABLE(ID INT,Name VARCHAR(100),TimeOfEvent DATETIME,EventCategory INT,TeamColor VARCHAR(10));
INSERT INTO @tbl VALUES
 (1,'Verlene Bucy','2015-01-30T09:10:00.000',1,'Blue')
,(2,'Geneva Rendon','2015-01-30T09:20:00.000',2,'Blue')
,(3,'Juliane Hartwig','2015-01-30T09:25:00.000',3,'Blue')
,(4,'Vina Dutton','2015-01-30T12:55:00.000',2,'Red')
,(5,'Cristin Lewis','2015-01-30T15:50:00.000',2,'Red')
,(6,'Reiko Cushman','2015-01-30T17:10:00.000',1,'Red')
,(7,'Mallie Temme','2015-01-30T18:35:00.000',3,'Blue')
,(8,'Keshia Seip','2015-01-30T19:55:00.000',2,'Blue')
,(9,'Rosalia Maher','2015-01-30T20:35:00.000',3,'Red')
,(10,'Keven Gabel','2015-01-30T21:25:00.000',3,'Red');

WITH Extended AS
(
    SELECT *
          ,DATEDIFF(MINUTE,'2015-01-30T21:26:00.000',TimeOfEvent) AS MinuteDiff --use GETDATE() here...
          ,COUNT(*) OVER(PARTITION BY EventCategory) AS CountCategory
    FROM @tbl AS tbl
)
,Filtered24Hours AS
(
    SELECT *
    FROM Extended
    WHERE CountCategory >=4
)
,Filtered60Mins AS
(
    SELECT *
    FROM Extended
    WHERE MinuteDiff >=-60 
      AND CountCategory >=2
)
SELECT DISTINCT (SELECT COUNT(*) FROM Filtered24Hours AS x WHERE x.EventCategory=outerSource.EventCategory) AS CountNames
               ,'per24h' AS TimeIntervall
               ,STUFF((
                        SELECT ' ,' + innerSource.Name 
                        FROM Filtered24Hours AS innerSource
                        WHERE innerSource.EventCategory=outerSource.EventCategory
                        ORDER BY innerSource.TimeOfEvent
                        FOR XML PATH('') 
                       ),1,2,'') AS Names 
              ,EventCategory 
              ,NULL
FROM Filtered24Hours AS outerSource
UNION 
SELECT DISTINCT (SELECT COUNT(*) FROM Filtered60Mins AS x WHERE x.EventCategory=outerSource.EventCategory) 
               ,'per1h'
               ,STUFF((
                        SELECT ' ,' + innerSource.Name 
                        FROM Filtered60Mins AS innerSource
                        WHERE innerSource.EventCategory=outerSource.EventCategory
                        ORDER BY innerSource.TimeOfEvent
                        FOR XML PATH('') 
                       ),1,2,'') 
              ,EventCategory 
              ,TeamColor
FROM Filtered60Mins AS outerSource