0

我无法在 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

4 回答 4

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

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

于 2013-07-23T06:37:33.353 回答
0
SELECT @Departure, @Arrival, DATEDIFF(hh, @Departure, @Arrival) AS NumofHaltHrs 
  FROM TABLE
 WHERE YOR_WHERE_CONDITION

我不确定您是否想要这种格式的结果。看看这是否有帮助。

于 2013-07-23T06:58:15.750 回答
0

这是 SQLFiddel 演示

以下是查询:

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在输出中添加您想要的表中的暂停条目数。

于 2013-07-23T07:48:05.763 回答
0

像这样的东西应该可以工作,使用公共表表达式(并假设 NumHaltHrs 来自具有某种 id 或 time to order by 的表)

;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

一个用于测试的 SQLfiddle

于 2013-07-23T16:08:14.683 回答