SQL为记录之间的每个日期生成行

时间:2015-01-30 15:58:28

标签: sql sql-server-2008

我正在尝试创建一个返回员工状态记录的SQL查询,以便稍后按小时分组。我的查询将在特定日期运行。

数据样本如下

recID | EmployeeID |  StatusDate          |   Status  
100   |  1000      | 2015-01-20 09:01 AM  | logged-in
101   |  1000      | 2015-01-20 09:07 AM  | Break
102   |  2000      | 2015-01-20 09:08 AM  | logged-in
103   |  1000      | 2015-01-20 09:09 AM  | logged-in
104   |  1000      | 2015-01-20 09:11 AM  | Logged-Off
105   |  2000      | 2015-01-20 09:12 AM  | Logged-Off

我想要得到的是每分钟的记录列表,以说明员工状态。结果如下

EmployeeID |  StatusDate          |   Status  
 1000      | 2015-01-20 09:01 AM  | logged-in
 1000      | 2015-01-20 09:02 AM  | logged-in
 1000      | 2015-01-20 09:03 AM  | logged-in
 1000      | 2015-01-20 09:04 AM  | logged-in
 1000      | 2015-01-20 09:05 AM  | logged-in
 1000      | 2015-01-20 09:06 AM  | logged-in
 1000      | 2015-01-20 09:07 AM  | Break
 1000      | 2015-01-20 09:08 AM  | Break
 2000      | 2015-01-20 09:08 AM  | logged-in
 1000      | 2015-01-20 09:09 AM  | logged-in
 2000      | 2015-01-20 09:09 AM  | logged-in
 1000      | 2015-01-20 09:10 AM  | logged-in
 2000      | 2015-01-20 09:10 AM  | logged-in
 1000      | 2015-01-20 09:11 AM  | Logged-Off
 2000      | 2015-01-20 09:11 AM  | logged-in
 2000      | 2015-01-20 09:12 AM  | Logged-Off
 1000      | 2015-01-20 09:12 AM  | Logged-Off

最终结果将计算每分钟携带状态的用户(员工)数量,因此结果如下:

DateTime             | Status      | Count
2015-01-20 09:01 AM  | logged-in   | 1
2015-01-20 09:02 AM  | logged-in   | 1
2015-01-20 09:03 AM  | logged-in   | 1
2015-01-20 09:04 AM  | logged-in   | 1
2015-01-20 09:05 AM  | logged-in   | 1
2015-01-20 09:06 AM  | logged-in   | 1
2015-01-20 09:07 AM  | Break       | 1
2015-01-20 09:08 AM  | Break       | 1
2015-01-20 09:08 AM  | logged-in   | 1
2015-01-20 09:09 AM  | logged-in   | 2
2015-01-20 09:10 AM  | logged-in   | 2
2015-01-20 09:11 AM  | Logged-Off  | 1
2015-01-20 09:11 AM  | logged-in   | 1
2015-01-20 09:12 AM  | Logged-Off  | 2

非常感谢任何指导。 干杯!

2 个答案:

答案 0 :(得分:0)

你有什么尝试?

这看起来很简单:

Select DateTime, Status, Count(*)
...
Group by DateTime, Status

...

答案 1 :(得分:0)

2008年有点难,但现在是:

DECLARE @Fact TABLE
    (
      EmpID INT ,
      dt DATETIME ,
      St NVARCHAR(MAX)
    )

INSERT  INTO @Fact
VALUES  ( 1000, '2015-01-20 09:01', 'logged-in' ),
        ( 1000, '2015-01-20 09:07', 'Break' ),
        ( 2000, '2015-01-20 09:08', 'logged-in' ),
        ( 1000, '2015-01-20 09:09', 'logged-in' ),
        ( 1000, '2015-01-20 09:11', 'Logged-Off' ),
        ( 2000, '2015-01-20 09:12', 'Logged-Off' )


DECLARE @dates TABLE ( dt DATETIME )
DECLARE @count INT , @minDT DATETIME , @maxDT DATETIME


SELECT  @minDT = MIN(dt), @maxDT = MAX(dt) FROM @Fact

WHILE @minDT <= @maxDT
    BEGIN
        INSERT  INTO @dates
        VALUES  ( @minDT )
        SET @minDT = DATEADD(mi, 1, @minDT)
    END;

WITH    cte
          AS ( SELECT   c.EmpID, d.dt, a.St
               FROM     @dates d
                        CROSS JOIN ( SELECT DISTINCT EmpID FROM   @Fact ) c
                        CROSS APPLY ( SELECT    St
                                      FROM      @Fact f
                                      WHERE     f.EmpID = c.EmpID
                                                AND dt = ( SELECT MAX(dt)
                                                           FROM @Fact fi
                                                           WHERE fi.EmpID = c.EmpID AND fi.dt <= d.dt )
                                    ) a
             )
    SELECT  dt, St, COUNT(*) AS Count
    FROM    cte
    GROUP BY dt, St
    ORDER BY dt, St DESC

输出:

dt                      St          Count
2015-01-20 09:01:00.000 logged-in   1
2015-01-20 09:02:00.000 logged-in   1
2015-01-20 09:03:00.000 logged-in   1
2015-01-20 09:04:00.000 logged-in   1
2015-01-20 09:05:00.000 logged-in   1
2015-01-20 09:06:00.000 logged-in   1
2015-01-20 09:07:00.000 Break   1
2015-01-20 09:08:00.000 logged-in   1
2015-01-20 09:08:00.000 Break   1
2015-01-20 09:09:00.000 logged-in   2
2015-01-20 09:10:00.000 logged-in   2
2015-01-20 09:11:00.000 Logged-Off  1
2015-01-20 09:11:00.000 logged-in   1
2015-01-20 09:12:00.000 Logged-Off  2