0

我正在处理一个非常奇怪的 SQL 问题,我必须比较以前的行

Number    start_date                      end_date    
 -----     -------                      ------------
 1    2011-06-07 00:00:00.000         2011-07-10 00:00:00.000   
 2    2011-10-11 00:00:00.000         2011-10-11 00:00:00.000   
 3    2011-10-26 00:00:00.000         2011-10-29 00:00:00.000   
 4    2011-10-29 00:00:00.000         2011-11-15 00:00:00.000   

在这里,我必须比较两条不同线上的 start_date 和 end_date 并从中创建一个视图。(如果 start_date 小于前一个 end_date ,则条件设置为 1)。

那么它应该比较 2011-10-26 00:00:00.000 3 和 2011-10-27 00:00:00.000 2 30 天

Number    start_date                      end_date                 Criteria
-----     -----------                   ----------------          ------------
1   2011-06-07 00:00:00.000       2011-07-10 00:00:00.000            0
2   2011-10-11 00:00:00.000       2011-10-11 00:00:00.000            0
3   2011-10-26 00:00:00.000       2011-10-29 00:00:00.000            1
4   2011-10-30 00:00:00.000       2011-11-15 00:00:00.000            1   

我很困惑我应该如何进行。

任何帮助都会有所帮助!!!!

谢谢 !!!

4

3 回答 3

2

最直接的方法是使用子查询:

select A.number, a.start_date, a.end_date,
    CASE WHEN start_date < dateadd(d,30,(select TOP(1) b.end_date
     from mytable B
     where B.number < A.number
     order by B.number desc)) then 1 else 0 end Criteria
from mytable A

注意:如果开始日期是上一行结束日期之后的第 29 天,Criteria则变为 1。到第 30 天之后,为 0。30根据需要调整查询中的 。

样本:

create table mytable (
    Number int primary key,
    start_date datetime,
    end_date datetime);

insert mytable
select 1, '2011-06-07', '2011-07-10' union all
select 2, '2011-10-11', '2011-10-27' union all
select 3, '2011-10-26', '2011-10-29' union all
select 4, '2011-10-29', '2011-11-15'

结果:

number  start_date  end_date    Criteria
1   2011-06-07 00:00:00.000 2011-07-10 00:00:00.000 0
2   2011-10-11 00:00:00.000 2011-10-27 00:00:00.000 0
3   2011-10-26 00:00:00.000 2011-10-29 00:00:00.000 1
4   2011-10-29 00:00:00.000 2011-11-15 00:00:00.000 0
于 2012-09-18T20:52:48.473 回答
1

尝试case像这样使用:

create view vDates as
select Number,start_date,end_date, 
       case 
         when start_date<end_date 
           then 0
         else 1
       end as Criteria
from tab

SQL 小提琴演示

于 2012-09-18T20:56:47.050 回答
0

一种更易读的方法是创建一个函数并发送正确的日期:

功能:

create function [dbo].[CompareDates] (
    @START_DATE datetime,
    @PREVIOUS_END_DATE datetime
)
RETURNS int
AS
BEGIN

    if @START_DATE < @PREVIOUS_END_DATE
        return 1
    return 0

END

查询(使用子查询):

declare @dates table
(
    number int,
    start datetime,
    end_date datetime
)

insert into @dates values
(1, '2011-06-07 00:00:00.000', '2011-07-10 00:00:00.000'),
(2, '2011-10-11 00:00:00.000', '2011-10-27 00:00:00.000'),
(3, '2011-10-26 00:00:00.000', '2011-10-29 00:00:00.000'),
(4, '2011-10-29 00:00:00.000', '2011-11-15 00:00:00.000')

select *, dbo.CompareDates(dates.end_date, dates.previous_end_date)  from
(
    select number, start, end_date,
        (select TOP 1 end_date
        from @dates d2
        where d2.number < d1.number
        order by d2.number desc) as previous_end_date
    from @dates d1
) dates
于 2012-09-19T11:18:54.043 回答