0

我有两个 CTE,我想将它们组合在一起。我尝试了很多,但我遇到了语法错误。第一部分:

declare @TimeRanges as TABLE (SessionStart datetime, SessionEnd datetime);

     with TimeRanges as (
  select @Start as StartTime, @Start + @TimeRange as EndTime
  union all
  select StartTime + @TimeRange, EndTime + @TimeRange
    from TimeRanges
    where EndTime  < @Finish )

这是第二部分:

;with cte as
(
    select SessionStartTime as changetime,1 as CC from Calls
    union all
    select SessionCloseTime,-1 from Calls
)
    select top 1 changetime,rt from
    (
    select * from cte
        cross apply 
        (select SUM(cc) as rt from cte c where c.changetime<=cte.changetime) rt         
    ) v
    order by rt desc

我想做的事:

    @Start datetime, 
    @Finish datetime,
    @TimeRange time
AS
BEGIN

    SET NOCOUNT ON;
    declare @res int SET @res = 0

    declare @TimeRanges as TABLE (SessionStart datetime, SessionEnd datetime);

    with TimeRanges as 
     (  select @Start as StartTime, @Start + @TimeRange as EndTime
        union all  
        select StartTime + @TimeRange, EndTime + @TimeRange
        from TimeRanges
        where EndTime  < @Finish ),


    cte as
    (
    select SessionStart as changetime,1 as CC from TimeRanges
    union all
    select SessionEnd,-1 from TimeRanges
    )
    select top 1 changetime,rt from
    (
    select * from cte
        cross apply 
        (select SUM(cc) as rt from cte c where c.changetime<=cte.changetime) rt         
    ) v
    order by rt desc


    select StartTime, EndTime,cte.rt
    from TimeRanges as TR left outer join
      dbo.Test as Test on TR.StartTime <= Test.SessionStartTime 
      and Test.SessionCloseTime < TR.EndTime      
    where Test.ScenarioID = 24 
    group by TR.StartTime, TR.EndTime,cte.rt    
END

第一个 CTE,根据 StartTime 和 EndTime 之间的 @timerange 对时间进行分组或拆分。例如,StartTime 11:00 EndTime 11:10 和 TimeRange 05:00(5 分钟)然后将它们分成两部分:11:00 - 11:05 和 11:05 - 11:10。第二个 CTE 为每个这些范围计算一些东西。在这里不重要。我试图将它们结合起来,但出现错误:

列名“SessionStart”无效

无效的对象名称“时间范围”

4

1 回答 1

0

因为在TimeRangesCTE 中,您对列的命名不同:

with TimeRanges as 
 (  select @Start as StartTime, @Start + @TimeRange as EndTime --StartTime and EndTime
    union all  
    select StartTime + @TimeRange, EndTime + @TimeRange
    from TimeRanges
    where EndTime  < @Finish ),


cte as
(
select StartTime as changetime,1 as CC from TimeRanges --StartTime, not SessionStart
union all
select EndTime,-1 from TimeRanges --EndTime
)
select top 1 changetime,rt from
(
select * from cte
    cross apply 
    (select SUM(cc) as rt from cte c where c.changetime<=cte.changetime) rt         
) v
order by rt desc

但是您随后尝试在第二个查询中再次引用 CTE。您不能这样做 - 每个 CTE 都适用于单个查询。

你可以重复一遍:

with TimeRanges as 
 (  select @Start as StartTime, @Start + @TimeRange as EndTime --StartTime and EndTime
    union all  
    select StartTime + @TimeRange, EndTime + @TimeRange
    from TimeRanges
    where EndTime  < @Finish )
select StartTime, EndTime,cte.rt
from TimeRanges as TR left outer join
  dbo.Test as Test on TR.StartTime <= Test.SessionStartTime 
  and Test.SessionCloseTime < TR.EndTime      
where Test.ScenarioID = 24 
group by TR.StartTime, TR.EndTime,cte.rt  
于 2012-10-30T08:23:54.960 回答