
実現したいこと
名前付きのウインドウ構文でエラーが起きずに正しくデータを抽出できるようにしたい
発生している問題・分からないこと
今現在SQLServerにおいて、計測日sample_date(日付型)、負荷量load_val(整数型)のテーブルLoadSampleから直前の計測日と負荷量を求めるSQL文をWINDOW構文を用いて作成しています。
下記の構文では動き、正しい値を得ることができました。
select sample_date as cur_date,
load_val as cur_load,
min(sample_date)
over (order by sample_date rows between 1 preceding and 1 preceding) as latest_date,
min(load_val)
over (order by sample_date rows between 1 preceding and 1 preceding) as latest_load
from loadsample;
しかし、名前付きのウインドウ構文を使用した下記の構文では不適切な構文がありますというエラーメッセージが発生しました。
select sample_date as cur_date,
load_val as cur_load,
min(sample_date) over W as latest_date,
min(load_val) over W as latest_load
from LoadSample
window W as (order by sample_date rows between 1 preceding and 1 preceding);
このエラーを解決するにはどのように構文を変更すればよいのでしょうか。
それとも、SQLServerでは名前付きのウインドウ構文が使用できないのでしょうか。
お手数ですが回答の方をお願いします。
エラーメッセージ
error
1メッセージ 102、レベル 15、状態 1、行 3 2'W' 付近に不適切な構文があります。 3
該当のソースコード
select sample_date as cur_date, load_val as cur_load, min(sample_date) over W as latest_date, min(load_val) over W as latest_load from LoadSample window W as (order by sample_date rows between 1 preceding and 1 preceding);
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
select sample_date as cur_date,
load_val as cur_load,
min(sample_date) over [W] as latest_date,
min(load_val) over [W] as latest_load
from LoadSample
window [W] as (order by sample_date rows between 1 preceding and 1 preceding);
Wを[]で囲んでみたが、結果は同じだった。
補足
特になし


回答1件
あなたの回答
tips
プレビュー