1
日変数の宣言を持つファンネルクエリのテーブルを作成したいとします。createステートメントを使用せずにクエリを使用すると、クエリはうまく動作します。コメントで指摘されていたとして、あなたのクエリがすべき新しいテーブルに日宣言してクエリを保存することができますどのようにMSSQLで変数宣言を使用したクエリに基づいてテーブルを作成する方法
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'declare'.
Msg 102, Level 15, State 1, Line 19
Incorrect syntax near ')'.
select * into tbl60D_AlertsFunnel from (
declare @d date = getdate() - 60
select sum(IP) AS IP ,sum(sentCount) sentCount, sum(readCount) readCount, sum(NumberOfOpensPerEmail) OpenNumber
from (
select COUNT(distinct CAST(sm.IP AS nvarchar(20))) as IP
, count(distinct v.iditem) sentCount
, MAX(cast(sm.OpenDate as date)) as OpenDateShort -- v.iditem is for sent, sm.OpenDate IS FOR OPEN
,count(cast(sm.OpenDate as date)) as NumberOfOpensPerEmail
, count(distinct sm.iditem) readCount
from fff.dbo.v_rep_MessageQueue v (nolock)
left join [FF].[dbo].[tblMessageOpenedSMTP] sm
on v.IdItem = sm.iditem
-- and cast(sm.OpenDate as date) > @d
where v.IdMessageType = 20
and V.DateScheduled > @d
group by sm.IP
) d
) j
クエリ内に変数を宣言することはできません。 –
この行を 'declare @d date = getdate() - 60'とし、**選択する前に**を入れてください。 –