mysql - 查找第一次出现的连续开始/结束列

标签 mysql sql-server tsql

我有一张包含就业日期的表格。每次发生变化时都会添加一个新行 - 薪水的变化是最常见的变化。因此,新行等于该人的最后 TO 日期加一 (1)。如果我的薪水在 2014-04-01 发生变化,我的前一行将在 2013-03-31 结束其 TO 日期,而我的新行将开始其 FROM 日期为 2014-04-01

我只想获取就业日期,而不是由于更改而导致的后续日期。看看这张表:

SSN         FROM        TO
----------------------------------
0987654321  2011-01-01  2011-12-31
0987654321  2012-01-01  2012-12-31
1234567890  2012-01-01  2012-12-31
0987654321  2013-01-01  2013-12-31
1234567890  2013-01-01  2013-06-30
0987654321  2014-01-01  2014-08-31
1234567890  2016-01-01  2016-12-31
1234567890  2017-01-01  2017-12-31
1234567890  2018-01-01  null

我想要的输出:

SSN         FROM        TO
----------------------------------
0987654321  2011-01-01  2014-08-31
1234567890  2012-01-01  2013-06-30
1234567890  2016-01-01  null

我想我可以创建一个比 TO 多一天的字段:

SELECT 
    SSN, TO, FROM, DATEADD(DAY, 1, TO) AS NEW 
FROM 
    table

但我不知道如何在不同的行上继续匹配 NEWTO。也许 WHERE NOT EXISTS 之类的?我不能让它工作。

然后我想我可以使用LAG但是默认情况下表中的上一行与下一行没有关系,我不能使用ORDER BY一个子查询。我不允许,不知道为什么(T-SQL?)。

仅供引用,我不能CREATE TABLEINSERT INTO TABLE 等,我也不能声明变量。我们将获得一个允许所有这些的模块,但现在我没有这些特权。

更新: 第一个答案实际上是正确的,但我注意到另一个干扰它的领域。一个 SSN 可以包含多个 ID,因此 ID 也必须拆分。这是我表中的实际数据。

CREATE TABLE Samples
    (
     SSN varchar(10), 
     ID varchar(4),
     FromDate Date, 
     ToDate Date
    );

INSERT INTO Samples
(SSN, ID, FromDate, ToDate)
VALUES
( '6612140000', '1000', '2005-01-01', '2005-03-31' ),
( '6612140000', '1000', '2005-04-01', '2005-09-30' ),
( '6612140000', '1000', '2005-10-01', '2006-03-31' ), 
( '6612140000', '2000', '2005-10-01', '2006-04-30' ),
( '6612140000', '1000', '2006-04-01', '2007-03-31' ),
( '6612140000', '1000', '2007-04-01', '2008-03-31' ),
( '6612140000', '1000', '2008-04-01', '2009-03-31' ),
( '6612140000', '1000', '2009-04-01', '2010-03-31' ),
( '6612140000', '1000', '2010-04-01', '2010-11-30' ),
( '6612140000', '1000', '2010-12-01', '2011-03-31' ),
( '6612140000', '1000', '2011-04-01', '2011-08-21' ),
( '6612140000', '1000', '2011-08-22', '2011-11-13' ),
( '6612140000', '1000', '2011-11-14', '2011-11-30' ),
( '6612140000', '1000', '2011-12-01', '2012-01-31' ),
( '6612140000', '1000', '2016-07-01', '2017-03-31' ),
( '6612140000', '1000', '2017-04-01', '2017-11-30' ),
( '6612140000', '1000', '2017-12-01', '2018-03-31' ),
( '6612140000', '1000', '2018-04-01', null ),
( '7605140000', '1000', '2013-11-01', '2013-11-30' ),
( '7605140000', '1000', '2013-12-01', '2013-12-31' ),
( '7605140000', '1000', '2014-01-01', '2014-03-31' ),
( '7605140000', '1000', '2014-04-01', '2014-12-31' ),
( '7605140000', '1000', '2015-05-01', '2015-05-31' ),
( '7605140000', '1000', '2015-06-01', '2015-09-30' ),
( '7605140000', '1000', '2015-10-01', '2015-10-31' ),
( '7605140000', '1000', '2016-01-25', '2016-07-24' ),
( '7605140000', '1000', '2016-07-25', '2016-08-31' ),
( '7605140000', '1000', '2016-09-01', '2017-03-31' ),
( '7605140000', '1000', '2017-04-01', '2017-11-30' ),
( '7605140000', '1000', '2017-12-01', null );

答案中的代码,我尝试将 ID 字段添加到,但没有成功:

with

  FromDates as (
    -- All of the   FromDates   for each   SSN   for which there is not
    --   a contiguous preceding period.
    select SO.SSN, SO.ID, SO.FromDate, SO.ToDate,
      Row_Number() over ( partition by SO.SSN order by SO.FromDate ) as RN
      from Samples as SO
      where not exists (
        select 42 from Samples as SI where SI.SSN = SO.SSN and SI.ID = SO.ID and
          SI.ToDate = DateAdd( day, -1, SO.FromDate ) ) ),

  ToDates as (
    -- All of the   ToDates   for each   SSN   for which there is not
    --   a contiguous following period.
    select SSN, ID, FromDate, ToDate, Row_Number() over ( partition by SSN order by FromDate ) as RN
      from Samples as SO
      where not exists (
        select 42 from Samples as SI where SI.SSN = SO.SSN and SI.ID = SO.ID and
          SI.FromDate = DateAdd( day, 1, SO.ToDate ) ) ),

  Ranges as (
    -- Pair the   FromDate   and   ToDate   entries for each   SSN .
    select F.SSN, F.ID, F.FromDate, T.ToDate
      from FromDates as F inner join
        ToDates as T on T.SSN = F.SSN and T.ID = F.ID and T.RN = F.RN ) 

-- Use any ONE of the following   select   statements to see what is going on:
-- select * from FromDates
--  select * from ToDates
  select * from Ranges 
  -- where SSN = '6612140000'
  order by SSN, ID, FromDate

返回:

SSN         ID      FromDate    ToDate
6612140000  1000    2016-07-01  (null)
7605140000  1000    2013-11-01  2014-12-31
7605140000  1000    2014-03-01  2014-12-31
7605140000  1000    2015-05-01  2015-10-31
7605140000  1000    2015-05-01  2015-10-31
7605140000  1000    2016-01-25  (null)

最佳答案

以下示例根据您的数据组装岛屿。通过更改启用/注释的最终 select 语句,您可以看到过程中的中间结果。

更新:更改了 CTE 中的日期比较,以便它们可以受益于 SSN, FromDateSSN, ToDate 的索引。

-- Sample data.
declare @Samples table ( SSN VarChar(10), FromDate Date, ToDate Date );
insert into @Samples ( SSN, FromDate, ToDate ) values
  ( '0987654321', '2011-01-01', '2011-12-31' ),
  ( '0987654321', '2012-01-01', '2012-12-31' ),
  ( '1234567890', '2012-01-01', '2012-12-31' ),
  ( '0987654321', '2013-01-01', '2013-12-31' ),
  ( '1234567890', '2013-01-01', '2013-06-30' ),
  ( '0987654321', '2014-01-01', '2014-08-31' ),
  ( '1234567890', '2016-01-01', '2016-12-31' ),
  ( '1234567890', '2017-01-01', '2017-12-31' ),
  ( '1234567890', '2018-01-01', null );
select *
  from @Samples;

-- Sample data made a little easier to read.
select *,
  case when exists (
    select 42 from @Samples as SI where SI.SSN = S.SSN and
      DateDiff( day, S.ToDate, SI.FromDate ) = 1 ) then 1 else 0 end as Continued
  from @Samples as S
  order by SSN, FromDate;

-- Process the data.
with
  FromDates as (
    -- All of the   FromDates   for each   SSN   for which there is not
    --   a contiguous preceding period.
    select SO.SSN, SO.FromDate, SO.ToDate,
      Row_Number() over ( partition by SO.SSN order by SO.FromDate ) as RN
      from @Samples as SO
      where not exists (
        select 42 from @Samples as SI where SI.SSN = SO.SSN and
          SI.ToDate = DateAdd( day, -1, SO.FromDate ) ) ),
  ToDates as (
    -- All of the   ToDates   for each   SSN   for which there is not
    --   a contiguous following period.
    select SSN, FromDate, ToDate, Row_Number() over ( partition by SSN order by FromDate ) as RN
      from @Samples as SO
      where not exists (
        select 42 from @Samples as SI where SI.SSN = SO.SSN and
          SI.FromDate = DateAdd( day, 1, SO.ToDate ) ) ),
  Ranges as (
    -- Pair the   FromDate   and   ToDate   entries for each   SSN .
    select F.SSN, F.FromDate, T.ToDate
      from FromDates as F inner join
        ToDates as T on T.SSN = F.SSN and T.RN = F.RN )
  -- Use any ONE of the following   select   statements to see what is going on:
--  select * from FromDates order by SSN, FromDate;
--  select * from ToDates order by SSN, FromDate;
  select * from Ranges order by SSN, FromDate;

当然,如果 SSN 中实际上有单独的 Id 值,这些值将被独立处理,答案将变为如下内容:

-- Sample data.
declare @Samples as Table ( SSN VarChar(10), Id VarChar(4), FromDate Date, ToDate Date );
insert into @Samples ( SSN, ID, FromDate, ToDate ) values
    ( '6612140000', '1000', '2005-01-01', '2005-03-31' ),
    ( '6612140000', '1000', '2005-04-01', '2005-09-30' ),
    ( '6612140000', '1000', '2005-10-01', '2006-03-31' ), 
    ( '6612140000', '2000', '2005-10-01', '2006-04-30' ),
    ( '6612140000', '1000', '2006-04-01', '2007-03-31' ),
    ( '6612140000', '1000', '2007-04-01', '2008-03-31' ),
    ( '6612140000', '1000', '2008-04-01', '2009-03-31' ),
    ( '6612140000', '1000', '2009-04-01', '2010-03-31' ),
    ( '6612140000', '1000', '2010-04-01', '2010-11-30' ),
    ( '6612140000', '1000', '2010-12-01', '2011-03-31' ),
    ( '6612140000', '1000', '2011-04-01', '2011-08-21' ),
    ( '6612140000', '1000', '2011-08-22', '2011-11-13' ),
    ( '6612140000', '1000', '2011-11-14', '2011-11-30' ),
    ( '6612140000', '1000', '2011-12-01', '2012-01-31' ),
    ( '6612140000', '1000', '2016-07-01', '2017-03-31' ),
    ( '6612140000', '1000', '2017-04-01', '2017-11-30' ),
    ( '6612140000', '1000', '2017-12-01', '2018-03-31' ),
    ( '6612140000', '1000', '2018-04-01', null ),
    ( '7605140000', '1000', '2013-11-01', '2013-11-30' ),
    ( '7605140000', '1000', '2013-12-01', '2013-12-31' ),
    ( '7605140000', '1000', '2014-01-01', '2014-03-31' ),
    ( '7605140000', '1000', '2014-03-01', '2014-12-31' ),
    ( '7605140000', '1000', '2014-04-01', '2014-12-31' ),
    ( '7605140000', '1000', '2015-05-01', '2015-05-31' ),
--  ( '7605140000', '1000', '2015-05-01', '2015-05-31' ), -- Duplicate row?!
    ( '7605140000', '1000', '2015-06-01', '2015-09-30' ),
--  ( '7605140000', '1000', '2015-06-01', '2015-09-30' ), -- Duplicate row?!
    ( '7605140000', '1000', '2015-10-01', '2015-10-31' ),
--  ( '7605140000', '1000', '2015-10-01', '2015-10-31' ), -- Duplicate row?!
    ( '7605140000', '1000', '2016-01-25', '2016-07-24' ),
    ( '7605140000', '1000', '2016-07-25', '2016-08-31' ),
    ( '7605140000', '1000', '2016-09-01', '2017-03-31' ),
    ( '7605140000', '1000', '2017-04-01', '2017-11-30' ),
    ( '7605140000', '1000', '2017-12-01', null );
select *
  from @Samples;

-- Sample data made a little easier to read.
select *,
  case when exists (
    select 42 from @Samples as SI where SI.SSN = S.SSN and SI.Id = S.Id and
      DateDiff( day, S.ToDate, SI.FromDate ) = 1 ) then 1 else 0 end as Continued
  from @Samples as S
  order by SSN, Id, FromDate;

-- Process the data.
with
  FromDates as (
    -- All of the   FromDates   for each   SSN   for which there is not
    --   a contiguous preceding period.
    select SO.SSN, SO.Id, SO.FromDate, SO.ToDate,
      Row_Number() over ( partition by SO.SSN, SO.Id order by SO.FromDate ) as RN
      from @Samples as SO
      where not exists (
        select 42 from @Samples as SI where SI.SSN = SO.SSN and SI.Id = SO.Id and
          SI.ToDate = DateAdd( day, -1, SO.FromDate ) ) ),
  ToDates as (
    -- All of the   ToDates   for each   SSN   for which there is not
    --   a contiguous following period.
    select SO.SSN, SO.Id, SO.FromDate, SO.ToDate,
      Row_Number() over ( partition by SSN, SO.Id order by FromDate ) as RN
      from @Samples as SO
      where not exists (
        select 42 from @Samples as SI where SI.SSN = SO.SSN and SI.Id = SO.Id and
          SI.FromDate = DateAdd( day, 1, SO.ToDate ) ) ),
  Ranges as (
    -- Pair the   FromDate   and   ToDate   entries for each   SSN .
    select F.SSN, F.Id, F.FromDate, T.ToDate
      from FromDates as F inner join
        ToDates as T on T.SSN = F.SSN and T.Id = F.Id and T.RN = F.RN )
  -- Use any ONE of the following   select   statements to see what is going on:
--  select * from FromDates order by SSN, Id, FromDate;
--  select * from ToDates order by SSN, Id, FromDate;
  select * from Ranges order by SSN, Id, FromDate;

关于mysql - 查找第一次出现的连续开始/结束列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50310449/

相关文章:

sql - 统计重复记录的总数

mysql - 用于修剪 char 字段的 SQL Server 全局设置

sql-server - T-SQL 中的 PRINT 语句

Mysql查询不使用主键

MySQL 三表之间的内连接语法

c# - 如何使用 Linq 查询和 EF 从具有多个字段的表中仅检索一个字段

sql - sql server存储过程中的多个表类型参数

python mysql 写入错误

mysql - 将数组数据输入数据库表时遇到困难

linq-to-sql - T-SQL 选择匹配 ISNUMERIC 并且也在指定范围内的值。 (加上 Linq-to-sql)