SQL查询和CTE查询的语法

时间:2012-09-17 18:50:11

标签: tsql syntax common-table-expression

我将在这篇帖子的末尾发布我的查询,但首先需要说明。请忽略列名和表名,但我在两个位置有语法错误。当我用我的CTE提出这个查询时,它告诉我必须首先用一个;'来终止先前的语句。然后我在CTE中找到一个列名称的别名,然后它说“多部分标识符" E.ActiveAGVs"无法受约束。'

我希望我能很好地解释我的问题。如果有人能够看到我想要做的事情并让我知道它是否会起作用或纠正我的语法错误,我会非常感激。

Select A.move_hour as 'Hour', 
       isnull(B.move_count,0) as 'Current_Count', 
       isnull(C.move_count,0) as '1_Day_Previous', 
       isnull(D.move_count,0) as '2_Day_Previous',
       ISNULL (E.ActiveAGVs,0) as 'Active AGV''s'
           --^ Error right here
from
   (select distinct(DATEPART(HH, Move_History.Move_Dt)) as move_hour 
   from Move_History 
   where Plant_Id = 1 and Building_Id = 1) as A
left outer join
   (select datepart(HH,Move_History.Move_Dt) as move_hour, 
           Move_History.Move_Cnt as move_count 
   from Move_History 
Group by datepart(HH,Move_History.Move_Dt), Move_Cnt) as B on A.move_hour = B.move_hour
left outer join
(select datepart(HH,Move_History.Move_Dt) as move_hour, Move_History.Move_Cnt as       move_count 
from Move_History 
Group by datepart(HH,Move_History.Move_Dt), Move_Cnt) as C on A.move_hour = C.move_hour
left outer join
(select datepart(HH,Move_History.Move_Dt) as move_hour, Move_History.Move_Cnt as move_count 
from Move_History 
Group by datepart(HH,Move_History.Move_Dt), Move_Cnt) as D on A.move_hour = D.move_hour;
with const as (
    select cast(cast(getdate() as date) as datetime) as midnight
    ),
allhours as (
    select 0 as m_hour, midnight as timestart, dateadd(hour, 1, midnight) as timeend from const union all
   ...
    select 23 as m_hour, dateadd(hour, 23, midnight) as timestart, dateadd(hour, 24, midnight) as timeend from const
   ) 
(select ah.m_hour,
   (sum(datediff(SECOND, timestart), ah.timeend else dt.End_Dt end)) 
   / 18000.0) * 5 as ActiveAGVs              
from allhours as ah
 left outer join AGV_Report as dt
 on ah.timestart< coalesce(dt.End_dt, getdate()) and
    ah.timeend >= dt.Begin_Dt
Group by datepart(SECOND,ah.hour), ah.timestart) as E on A.move_hour = E.move_hour
                                   --^ 'Incorrect syntax near "as"'
 where A.move_hour is not null
order by ah.m_hour asc

2 个答案:

答案 0 :(得分:1)

您需要为语句定义的所有CTE必须位于语句的开头。即使CTE仅用于其中一个子查询,语法仍然要求将它们放在整个语句的开头, not 在特定子查询的开头,它们是实际参考。

因此,您的陈述应该看起来像这样:

;  -- required if there are statements preceding
with const as (
    select cast(cast(getdate() as date) as datetime) as midnight
    ),
allhours as (
    select
       0 as m_hour,
       midnight as timestart,
       dateadd(hour, 1, midnight) as timeend
    from const
    union all
   ...
    select
       23 as m_hour,
       dateadd(hour, 23, midnight) as timestart,
       dateadd(hour, 24, midnight) as timeend
    from const
   )
Select A.move_hour as 'Hour', 
       isnull(B.move_count,0) as 'Current_Count', 
       isnull(C.move_count,0) as '1_Day_Previous', 
       isnull(D.move_count,0) as '2_Day_Previous',
       ISNULL (E.ActiveAGVs,0) as 'Active AGV''s'
from
   (
      select distinct
         DATEPART(HH, Move_History.Move_Dt) as move_hour 
      from Move_History 
      where Plant_Id = 1 and Building_Id = 1
   ) as A
left outer join
   (
      select
         datepart(HH,Move_History.Move_Dt) as move_hour, 
         Move_History.Move_Cnt as move_count 
      from Move_History 
      Group by datepart(HH,Move_History.Move_Dt), Move_Cnt
   ) as B on A.move_hour = B.move_hour
left outer join
   (
      select
         datepart(HH,Move_History.Move_Dt) as move_hour,
         Move_History.Move_Cnt as move_count 
      from Move_History 
      Group by datepart(HH,Move_History.Move_Dt), Move_Cnt
   ) as C on A.move_hour = C.move_hour
left outer join
   (
      select
         datepart(HH,Move_History.Move_Dt) as move_hour,
         Move_History.Move_Cnt as move_count 
      from Move_History 
      Group by datepart(HH,Move_History.Move_Dt), Move_Cnt
   ) as D on A.move_hour = D.move_hour
left outer join  -- assuming...
   (
      select
         ah.m_hour,
         (sum(datediff(SECOND, timestart), ah.timeend else dt.End_Dt end))
         / 18000.0) * 5 as ActiveAGVs              
      from allhours as ah
      left outer join AGV_Report as dt
         on ah.timestart < coalesce(dt.End_dt, getdate())
        and ah.timeend >= dt.Begin_Dt
      Group by datepart(SECOND,ah.hour), ah.timestart
   ) as E on A.move_hour = E.move_hour
where A.move_hour is not null
order by ah.m_hour asc

答案 1 :(得分:0)

公用表表达式(CTE)必须通过分号(;)与任何先前语句分隔。如果with之前没有语句,那么就不需要分号。

您可以在一个with内拥有多个CTE。一个例子如下:

with
  -- Counting numbers.
  Numbers as (
    select 1 as Number
    union all
    select Numbers.Number + 1
      from Numbers
      where Number < 10 ),
  -- Squares of counting numbers.
  Squares as (
    select Number, Number * Number as Square
      from Numbers )
  -- Result.
  select Number, Square
    from Squares

与其他想要帮助的人一样,我不知道你的SQL正在尝试做什么。