-3

この投稿の最後にクエリを投稿しますが、最初に説明が必要です。列名とテーブル名は無視してください。ただし、構文エラーが 2 か所あります。このクエリを 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
4

2 に答える 2

1

ステートメントに対して定義する必要があるすべての CTE は、ステートメントの先頭に配置する必要があります。CTE はサブクエリの 1 つだけで使用されますが、構文では、実際に参照される特定のサブクエリの先頭ではなく、ステートメント全体の先頭に配置する必要があります。

したがって、ステートメントはおそらく次のようになります。

;  -- 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
于 2012-09-17T20:06:25.767 に答える
0

共通テーブル式 (CTE) は、前のステートメントからセミコロン (;) で区切る必要があります。の前にステートメントwithがない場合、セミコロンは必要ありません。

1 つの 内に複数の CTE を持つことができますwith。以下に例を示します。

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 が何をしようとしているのか分かりません。

于 2012-09-17T19:26:46.427 に答える