在SQL中组合多个CTE

时间:2012-10-30 07:16:11

标签: tsql common-table-expression

我有两个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

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

无效的列名'SessionStart'

无效的对象名称'TimeRanges'

1 个答案:

答案 0 :(得分:0)

因为在TimeRanges 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 ),


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