尝试在两个单独的列和单独的行

时间:2016-12-07 21:48:21

标签: tsql

这是来自餐馆的销售点数据样本:一个人在一天内的所有时间都打开和关闭支票。我试图找到一个人关闭支票并打开另一支票之间的差距(基本上是他们每天没有打开支票的时间)。我使用的是Lead函数,但是没有考虑到其他人之后打开支票但是比其他支票更早关闭的情况(chk_num 499就是一个很好的例子)。我可以通过手动检查这些数据来判断从14:27:35到14:29:14.000和16:01:12.000到16:59:22.000没有打开检查,但我无法弄清楚如何在较大的数据集中自动完成。

我在SQL Server 2012上使用SQL Server Management Studio 11.0.2100.60来编写此查询。

对不起下面的格式。我无法找到如何插入简单表的好解释。



<table><tbody><tr><th>chk_num</th><th>employee</th><th>chkstarttm</th><th>chkendtm</th></tr><tr><td>473</td><td>20106</td><td>2013-05-15 12:26:44.000</td><td>2013-05-15 13:21:58.000</td></tr><tr><td>480</td><td>20106</td><td>2013-05-15 12:38:17.000</td><td>2013-05-15 13:49:07.000</td></tr><tr><td>483</td><td>20106</td><td>2013-05-15 12:45:54.000</td><td>2013-05-15 14:27:35.000</td></tr><tr><td>484</td><td>20106</td><td>2013-05-15 12:46:19.000</td><td>2013-05-15 14:09:52.000</td></tr><tr><td>495</td><td>20106</td><td>2013-05-15 13:19:58.000</td><td>2013-05-15 14:10:02.000</td></tr><tr><td>499</td><td>20106</td><td>2013-05-15 13:30:38.000</td><td>2013-05-15 13:56:58.000</td></tr><tr><td>503</td><td>20106</td><td>2013-05-15 13:40:01.000</td><td>2013-05-15 14:11:47.000</td></tr><tr><td>515</td><td>20106</td><td>2013-05-15 14:29:14.000</td><td>2013-05-15 14:40:25.000</td></tr><tr><td>517</td><td>20106</td><td>2013-05-15 14:32:32.000</td><td>2013-05-15 16:01:12.000</td></tr><tr><td>522</td><td>20106</td><td>2013-05-15 14:34:57.000</td><td>2013-05-15 14:41:31.000</td></tr><tr><td>521</td><td>20106</td><td>2013-05-15 14:34:57.000</td><td>2013-05-15 15:03:02.000</td></tr><tr><td>523</td><td>20106</td><td>2013-05-15 14:34:57.000</td><td>2013-05-15 15:02:32.000</td></tr><tr><td>519</td><td>20106</td><td>2013-05-15 14:34:57.000</td><td>2013-05-15 14:40:56.000</td></tr><tr><td>520</td><td>20106</td><td>2013-05-15 14:34:57.000</td><td>2013-05-15 14:41:53.000</td></tr><tr><td>525</td><td>20106</td><td>2013-05-15 15:00:17.000</td><td>2013-05-15 15:27:55.000</td></tr><tr><td>535</td><td>20106</td><td>2013-05-15 15:27:13.000</td><td>2013-05-15 15:43:48.000</td></tr><tr><td>536</td><td>20106</td><td>2013-05-15 15:29:54.000</td><td>2013-05-15 15:30:56.000</td></tr><tr><td>537</td><td>20106</td><td>2013-05-15 15:41:06.000</td><td>2013-05-15 15:41:42.000</td></tr><tr><td>548</td><td>20106</td><td>2013-05-15 16:59:22.000</td><td>2013-05-15 18:04:47.000</td></tr><tr><td>551</td><td>20106</td><td>2013-05-15 17:04:35.000</td><td>2013-05-15 18:29:35.000</td></tr><tr><td>564</td><td>20106</td><td>2013-05-15 18:03:22.000</td><td>2013-05-15 19:35:51.000</td></tr></tbody></table>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

以下是我认为您可以使用的解决方案:

    --Generated Output        
    StartRange                      EndRange
    2013-05-15 14:27:35.000         2013-05-15 14:29:14.000
    2013-05-15 16:01:12.000         2013-05-15 16:59:22.000

    DECLARE @CheckData AS TABLE
    (
        CheckNum INT,
        EmployeeNum INT,
        StartDate DATETIME,
        EndDate DATETIME
    )

    INSERT INTO @CheckData VALUES ('473', '20106', '2013-05-15 12:26:44.000', '2013-05-15 13:21:58.000') 
    INSERT INTO @CheckData VALUES ('480', '20106', '2013-05-15 12:38:17.000', '2013-05-15 13:49:07.000') 
    INSERT INTO @CheckData VALUES ('483', '20106', '2013-05-15 12:45:54.000', '2013-05-15 14:27:35.000') 
    INSERT INTO @CheckData VALUES ('484', '20106', '2013-05-15 12:46:19.000', '2013-05-15 14:09:52.000') 
    INSERT INTO @CheckData VALUES ('495', '20106', '2013-05-15 13:19:58.000', '2013-05-15 14:10:02.000') 
    INSERT INTO @CheckData VALUES ('499', '20106', '2013-05-15 13:30:38.000', '2013-05-15 13:56:58.000') 
    INSERT INTO @CheckData VALUES ('503', '20106', '2013-05-15 13:40:01.000', '2013-05-15 14:11:47.000') 
    INSERT INTO @CheckData VALUES ('515', '20106', '2013-05-15 14:29:14.000', '2013-05-15 14:40:25.000') 
    INSERT INTO @CheckData VALUES ('517', '20106', '2013-05-15 14:32:32.000', '2013-05-15 16:01:12.000') 
    INSERT INTO @CheckData VALUES ('522', '20106', '2013-05-15 14:34:57.000', '2013-05-15 14:41:31.000') 
    INSERT INTO @CheckData VALUES ('521', '20106', '2013-05-15 14:34:57.000', '2013-05-15 15:03:02.000') 
    INSERT INTO @CheckData VALUES ('523', '20106', '2013-05-15 14:34:57.000', '2013-05-15 15:02:32.000') 
    INSERT INTO @CheckData VALUES ('519', '20106', '2013-05-15 14:34:57.000', '2013-05-15 14:40:56.000') 
    INSERT INTO @CheckData VALUES ('520', '20106', '2013-05-15 14:34:57.000', '2013-05-15 14:41:53.000') 
    INSERT INTO @CheckData VALUES ('525', '20106', '2013-05-15 15:00:17.000', '2013-05-15 15:27:55.000') 
    INSERT INTO @CheckData VALUES ('535', '20106', '2013-05-15 15:27:13.000', '2013-05-15 15:43:48.000') 
    INSERT INTO @CheckData VALUES ('536', '20106', '2013-05-15 15:29:54.000', '2013-05-15 15:30:56.000') 
    INSERT INTO @CheckData VALUES ('537', '20106', '2013-05-15 15:41:06.000', '2013-05-15 15:41:42.000') 
    INSERT INTO @CheckData VALUES ('548', '20106', '2013-05-15 16:59:22.000', '2013-05-15 18:04:47.000') 
    INSERT INTO @CheckData VALUES ('551', '20106', '2013-05-15 17:04:35.000', '2013-05-15 18:29:35.000') 
    INSERT INTO @CheckData VALUES ('564', '20106', '2013-05-15 18:03:22.000', '2013-05-15 19:35:51.000') 

    SELECT *
    FROM
        (
            SELECT MAX(enddate) OVER(ORDER BY startdate) StartRange,
             lead(startdate) OVER(ORDER BY startdate) EndRange
            FROM @Checkdata
        ) AS c
    WHERE c.StartRange < c.EndRange