如果当前不可用,请从前一行获取日期值

时间:2018-06-25 05:36:47

标签: sql sql-server tsql

我需要获取SQL Server中以下情况的逻辑/查询:

Last Updated                              Time
2018-06-21 23:09:21.947                 21-06-2018
2018-06-21 23:09:21.947                 23:09
2018-06-22 00:14:02.877                 23:15
2018-06-22 00:29:13.660                 22-06-2018
2018-06-22 00:29:13.660                 23:15
2018-06-22 00:41:20.773                 23-06-2018
2018-06-22 00:41:20.773                 23:20
2018-06-22 02:58:44.160                 23:22
2018-06-22 02:59:40.097                 23:25
2018-06-22 03:00:07.770                 23:28

我具有上面的数据,我想得到如下:

Last Updated                            Time    Time/Date
2018-06-21 23:09:21.947            21-06-2018   21-06-2018 23:09
2018-06-22 00:14:02.877               23:15     21-06-2018 23:15
2018-06-22 00:29:13.660              22-06-2018 22-06-2018 23:15
2018-06-22 00:41:20.773              23-06-2018 23-06-2018 23:20
2018-06-22 02:58:44.160                 23:22   23-06-2018 23:22
2018-06-22 02:59:40.097                  23:25  23-06-2018 23:25
2018-06-22 03:00:07.770                 23:28   23-06-2018 23:28

如上所述,需要获取日期/时间列,如果没有日期,则根据最近更新的列获取前一个日期。 我的第一个问题,干杯!!

2 个答案:

答案 0 :(得分:1)

您可以使用基于集合的操作来执行此操作。这是一种方法:

select t1.last_updated, t1.time,
       (t2.time + ' ' + t1.time) as time_date     
from t outer apply
     (select top (1) t2.*
      from t2
      where t2.time like '%-%-%'
            t2.last_updated <= t.last_updated
      order by t2.last_updated desc
     ) t2
where time like '%:%'
group by last_updated;

答案 1 :(得分:0)

这完全基于您的数据。因此,您可能需要进行相应的更改。

declare @RawData as Table
(
    LastUpdate DateTime,
    Time Varchar(20)
)

declare @Data as Table
(
    RowId int,
    LastUpdate DateTime,
    Time Varchar(20)
)

declare @FinalData as Table
(
    LastUpdate DateTime,
    Time Varchar(20),
    [DateTime] DateTime 
)

Insert into @RawData values ('2018-06-21 23:09:21.947','21-06-2018')
Insert into @RawData values ('2018-06-21 23:09:21.947','23:09')
Insert into @RawData values ('2018-06-22 00:14:02.877','23:15')
Insert into @RawData values ('2018-06-22 00:29:13.660','22-06-2018')
Insert into @RawData values ('2018-06-22 00:29:13.660','23:15')
Insert into @RawData values ('2018-06-22 00:41:20.773','23-06-2018')
Insert into @RawData values ('2018-06-22 00:41:20.773','23:20')
Insert into @RawData values ('2018-06-22 02:58:44.160','23:22')
Insert into @RawData values ('2018-06-22 02:59:40.097','23:25')
Insert into @RawData values ('2018-06-22 03:00:07.770','23:28')

insert into @Data
Select ROW_NUMBER() OVER(ORDER BY LastUpdate ASC), * from @RawData 

Declare @Count INT
SET @Count = (Select count(*) from @Data)

DECLARE @intFlag INT
SET @intFlag = 1
WHILE (@intFlag <=@Count)
BEGIN
    Declare @Date varchar(10)
    Declare @FinalDate varchar(10)

    if((Select Len(trim(time)) from @data where RowId = @intFlag) =10)
    Begin
        SET @Date = (Select Convert(date, time, 104) from @data where RowId = @intFlag)
    end
    else
    Begin 
        if((select Len(trim(time)) from @Data where RowID = @intFlag -1 ) = 10)
        Begin
            insert into @FinalData
            Select LastUpdate, time, Convert(Datetime,@Date + ' ' + time) from @data where RowID = @intFlag
        End
        else
        Begin
            insert into @FinalData
            Select LastUpdate, time, Convert(Datetime,@Date + ' ' + time) from @data where RowID = @intFlag
        End

    End 
    SET @intFlag = @intFlag + 1
END

Select * from @FinalData

GO