使用SQL重复下一行中的列值

时间:2013-07-23 05:32:25

标签: sql sql-server sql-server-2008

我无法在SQL中解决此循环结构问题。我有两个DATETIME参数:@Departure@Arrival。我正在添加NumberofHaltHrs我需要循环@Departure和@Arrival,如下所示。

假设我有

@Departure= 13/01/01 00:00:00(YY/MM/DD) and 
@Arrival=  13/01/10 02:00:00(YY/MM/DD)
NumofHaltHrs are like 2, 22, 26, 56 (this is a column of other table where I need to ass to result table)

期望的输出:

      @Departure                     @Arrival
    13/01/01 00:00:00          13/01/01 02:00:00  //Adding NumOfHaltHrs (2Hrs )
    13/01/01 02:00:00          13/01/02 00:00:00  //NumOfHaltHrs (22 Hrs)
    13/01/02 00:00:00          13/01/03 02:00:00
    13/01/03 02:00:00          13/01/05 08:00:00
    13/01/05 08:00:00          ...
    ...

我需要将@Departure,@ Arrival,NumOfHaltHrs插入到结果表中,其结构为

JourneyDetailsTable: (JourneyID,HaltID,Departure,Arrival,NumOfHaltHrs)
Halt : HaltID,NumOfHaltDays

我尝试了循环,但我无法在出发列中获取到达日期。

4 个答案:

答案 0 :(得分:1)

试试这个:

DECLARE @Departure DATETIME

SET 
    @Departure = '01/13/01 00:00:00'

CREATE TABLE #NumofHaltHrs(HaltTime INT, ID INT IDENTITY(1,1))

INSERT INTO #NumofHaltHrs
    (HaltTime)
VALUES
    (2)
INSERT INTO #NumofHaltHrs
    (HaltTime)
VALUES
    (22)
INSERT INTO #NumofHaltHrs
    (HaltTime)
VALUES
    (26)
INSERT INTO #NumofHaltHrs
    (HaltTime)
VALUES
    (56)

DECLARE @UpdatedArrivalTime DATETIME, @NumberOfHaltRows INT, @NumberOfHaltRowsIndex INT
SET @UpdatedArrivalTime = @Departure
SET @NumberOfHaltRowsIndex = 1

SELECT
    @NumberOfHaltRows = COUNT(ID)
FROM #NumofHaltHrs

CREATE TABLE #Schedule(DEPARTURE DATETIME, ARRIVAL DATETIME)

WHILE @NumberOfHaltRowsIndex <= @NumberOfHaltRows
BEGIN
    DECLARE @HaltTime INT
    SELECT
        @HaltTime = HaltTime
    FROM #NumofHaltHrs
    WHERE ID = @NumberOfHaltRowsIndex

    INSERT INTO #Schedule
    VALUES
        (@UpdatedArrivalTime, DATEADD(hour, @HaltTime, @UpdatedArrivalTime))

    SET @UpdatedArrivalTime = DATEADD(hour, @HaltTime, @UpdatedArrivalTime)

    SET @NumberOfHaltRowsIndex = @NumberOfHaltRowsIndex + 1
END

SELECT * FROM #Schedule

我不知道你想在哪种情况下使用它,所以如果数据很大,请检查性能。

答案 1 :(得分:0)

SELECT @Departure, @Arrival, DATEDIFF(hh, @Departure, @Arrival) AS NumofHaltHrs 
  FROM TABLE
 WHERE YOR_WHERE_CONDITION

我不确定您是否希望以此格式获得结果。看看这是否有帮助。

答案 2 :(得分:0)

<强> Here is the SQLFiddel Demo

以下是查询:

create table Halts (id int,NoOfHalts int)    
Go
insert into halts values(1,2),(2,22),(3,2),(4,22)
Go
select DATEADD(hour,
               T.sum - T.NoofHalts,
               Convert(datetime,'23/07/2012 00:00:00',103)) as Deaprture,
       DATEADD(hour,
               T.sum,
               Convert(datetime,'23/07/2012 00:00:00',103)) as Arrival ,NoofHalts
  from (select top (select COUNT(*) from Halts) 
               t1.id, 
               t1.NoOfHalts, 
               SUM(t2.NoOfHalts) as sum
          from Halts t1
         inner join Halts t2 on t1.id >= t2.id
         group by t1.id, t1.NoOfHalts
         order by t1.id
       ) T

在输出中添加Halts表格中的Halts条目数。

答案 3 :(得分:0)

这样的东西应该可以使用公共表表达式(并且假设NumHaltHrs来自具有某种id或时间的表来排序)

;WITH cte AS (
  SELECT @Departure dtime, 0 NumOfHaltHrs, 0 rn
  UNION 
  SELECT DATEADD(hour, (SELECT SUM(NumOfHaltHrs) 
                        FROM bop a 
                        WHERE a.idHalt<=bop.idHalt), @departure),
         NumOfHaltHrs,
         ROW_NUMBER() OVER (ORDER BY idHalt)
  FROM bop
)
SELECT a.rn+1 HaltId, a.dtime Departure, 
       COALESCE(b.dtime, @Arrival) Arrival,
       COALESCE(b.numofhalthrs, 0) NumOfHaltHrs
FROM cte a LEFT JOIN cte b ON a.rn = b.rn-1

An SQLfiddle to test with