0

2つの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 )

これが2番目の部分です:

;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 EndTime11:10とTimeRange05:00(5 min)は、これらを11:00-11:05と11:05-11:10の2つの部分に分割します。2番目のCTEは、これらの範囲ごとに何かをカウントします。ここでは重要ではありません。それらを組み合わせようとしましたが、エラーが発生します。

無効な列名'SessionStart'

無効なオブジェクト名'TimeRanges'

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

しかし、2 番目のクエリで 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 に答える