CTE(Common Table Expression)テーブルから派生した値を変数に取り込もうとしました。何らかの理由で動作しないようです。以下はコードです共通テーブル式の後に変数を使用する
WITH CTE
as
(
select
Case
when target_title like '%P1%' then 'P1'
when target_title like '%P2%' then 'P2'
when target_title like '%P3%' then 'P3'
when target_title like '%P4%' then 'P4'
End as Priority,
flag,
case when flag='Response' then Business_Hours_MMTR end as Response,
case when flag = 'Resolution' then Business_Hours_MMTR end as Resolution
from Keurig..Response_Resolution
where [Group] like '%Network%'
and datepart(month, Measurement_Stop) = datepart(month, Getdate())-1
and (Target_Title like '%P1%' OR Target_Title like '%P2%' OR Target_Title like '%P3%' OR Target_Title like '%P4%')
)
Declare @K4Resp numeric(5,2);
Select @K4Resp = CAST(AVG(Response) as numeric(10,2)) as K4Response from CTE where flag = 'Response' and Priority = 'P4'
group by Priority, flag
「CTE」の後の最初のステートメントは、「選択」ステートメントでなければなりません。あなたの変数をあなたの 'CTE'の上に宣言してください –
結果からランダムな' AVG(Response) 'が' @ K4Resp'に割り当てられます –
あなたの宣言の配置の他に、ここにいくつかの問題があります。変数は数値(5,2)として定義しますが、クエリでは数値(10,2)としてキャストします。また、スカラ変数の値を複数の行を持つクエリの結果に設定しています。 –