2012-10-30 78 views
0

我有兩個CTE,我想將它們組合在一起。我嘗試了很多,但是出現了語法錯誤。第一部分:在TSQL中結合多個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,團體或拆分時間按照開始時間之間的@timerange和EndTime。例如,開始時間11:00結束時間11:10和時間範圍05:00(5分鐘)然後將它們分爲兩部分:11:00 - 11:05和11:05 - 11:10。第二個CTE爲每個這些範圍計算一些東西。這裏不重要。我試圖把它們混合起來,但我到達那裏的錯誤:

無效的列名稱SessionStart「

無效的對象名稱TimeRanges」

回答

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 
+0

感謝Damien_The_Unbeliever。我糾正了它,但我不能合併兩個單獨的ctes的兩個查詢。 – cihata87