检查第一行日期是否在下一行日期之间
check the first rows date is in between the next rows date
每行有一个 DateEff
和一个 DateExp
。假设我 return 5 行。我需要检查第一行的 DateEff
以查看它是否位于第二、第三、第四和第五行的 DateEff
和 DateExp
之间,依此类推。我需要检查每个 DateEff
以确保它不在任何行 DateEff
和 DateExp
之间。
这是数据的示例。如您所见,第 3 行 DateEff
是 (2013-03-30),它位于第 4 行 DateEff
和 DateExp
以及第 5 行 DateEff
和`DateExp 之间。
Table
rowid DateEff DateExp
1 1969-01-01 2012-09-30
2 2012-10-01 2012-12-31
3 2013-03-30 2014-12-31
4 2013-01-01 2015-02-10
5 2013-01-01 2999-01-01
结果应该是这样的
Prob Id Problem Date Affected Id Aff Date Range
3 2013-03-30 4 2013-01-01 - 2015-02-10
3 2013-03-30 5 2013-01-01 - 2999-01-01
将 table 与自身连接到 return 重叠的行对:
Select a.RowID, a.DateEff as ProblemDate
, b.RowID as OverlapID, b.DateEff as OverlapStart, b.DateExp as OverlapEnd
from MyTable a
left join MyTable b
on a.RowID <> b.RowID
and a.DateEff <= b.DateExp
and a.DateEff >= b.DateEff
我认为这对你有用:
select
[Prob Id] = t.rowid,
[Problem Date] = t.DateEff,
[Affected Id] = a.rowid,
[Aff Date Range] = concat(a.DateEff,' - ',a.DateExp)
from tbl t -- your table is called tbl
outer apply
(
select *
from tbl -- your table is called tbl
where t.DateEff between dateeff and DateExp and rowid > t.rowid
) a
where a.DateEff is not null
order by t.rowid, t.DateEff;
根据您的示例数据,结果如下:
Prob Id Problem Date Affected Id Aff Date Range
3 2013-03-30 4 2013-01-01 - 2015-02-10
3 2013-03-30 5 2013-01-01 - 2999-01-01
4 2013-01-01 5 2013-01-01 - 2999-01-01
要从您的示例中获得准确的输出(不包括第 4 行),请将应用中的条件更改为 t.DateEff > dateeff and t.DateEff < DateExp and rowid > t.rowid
。输出将是:
Prob Id Problem Date Affected Id Aff Date Range
3 2013-03-30 4 2013-01-01 - 2015-02-10
3 2013-03-30 5 2013-01-01 - 2999-01-01
每行有一个 DateEff
和一个 DateExp
。假设我 return 5 行。我需要检查第一行的 DateEff
以查看它是否位于第二、第三、第四和第五行的 DateEff
和 DateExp
之间,依此类推。我需要检查每个 DateEff
以确保它不在任何行 DateEff
和 DateExp
之间。
这是数据的示例。如您所见,第 3 行 DateEff
是 (2013-03-30),它位于第 4 行 DateEff
和 DateExp
以及第 5 行 DateEff
和`DateExp 之间。
Table
rowid DateEff DateExp
1 1969-01-01 2012-09-30
2 2012-10-01 2012-12-31
3 2013-03-30 2014-12-31
4 2013-01-01 2015-02-10
5 2013-01-01 2999-01-01
结果应该是这样的
Prob Id Problem Date Affected Id Aff Date Range
3 2013-03-30 4 2013-01-01 - 2015-02-10
3 2013-03-30 5 2013-01-01 - 2999-01-01
将 table 与自身连接到 return 重叠的行对:
Select a.RowID, a.DateEff as ProblemDate
, b.RowID as OverlapID, b.DateEff as OverlapStart, b.DateExp as OverlapEnd
from MyTable a
left join MyTable b
on a.RowID <> b.RowID
and a.DateEff <= b.DateExp
and a.DateEff >= b.DateEff
我认为这对你有用:
select
[Prob Id] = t.rowid,
[Problem Date] = t.DateEff,
[Affected Id] = a.rowid,
[Aff Date Range] = concat(a.DateEff,' - ',a.DateExp)
from tbl t -- your table is called tbl
outer apply
(
select *
from tbl -- your table is called tbl
where t.DateEff between dateeff and DateExp and rowid > t.rowid
) a
where a.DateEff is not null
order by t.rowid, t.DateEff;
根据您的示例数据,结果如下:
Prob Id Problem Date Affected Id Aff Date Range
3 2013-03-30 4 2013-01-01 - 2015-02-10
3 2013-03-30 5 2013-01-01 - 2999-01-01
4 2013-01-01 5 2013-01-01 - 2999-01-01
要从您的示例中获得准确的输出(不包括第 4 行),请将应用中的条件更改为 t.DateEff > dateeff and t.DateEff < DateExp and rowid > t.rowid
。输出将是:
Prob Id Problem Date Affected Id Aff Date Range
3 2013-03-30 4 2013-01-01 - 2015-02-10
3 2013-03-30 5 2013-01-01 - 2999-01-01