我正在尝试使用Teradata Studio运行以下Teradata(V 14.10.3.09)数据库查询:
WITH params AS
(
SELECT
TO_TIMESTAMP(TO_CHAR(CURRENT_TIMESTAMP, 'yyyy-mm-dd hh24'),'yyyy-mm-dd hh24') AS w0s -- truncate to the current hour
,CURRENT_TIMESTAMP AS w0e
,CURRENT_DATE -8 AS w1s
,CURRENT_DATE -1 AS w1e
)
SELECT ST_NM
,SUM(CASE WHEN CRT_DTTM>=w0s AND CRT_DTTM<w0e THEN 1 ELSE 0 END) AS Call_Volume_This_hr
,SUM(CASE WHEN CRT_DTTM>=w1s AND CRT_DTTM<w1e THEN 1 ELSE 0 END)/7.0 AS Avg_Call_Volume_Past_Week1
,CASE WHEN Call_Volume_This_hr < Avg_Call_Volume_Past_Week1 THEN 0
ELSE Call_Volume_This_hr -Avg_Call_Volume_Past_Week1
END AS VARIANCE
,(100 * VARIANCE) / Avg_Call_Volume_Past_Week1 AS Percentage_Variance
FROM XDW_V.INB_CALL_NRT_RPT
WHERE CRT_DTTM>=w1s
AND EXTRACT(HOUR FROM CRT_DTTM)=EXTRACT( HOUR FROM CURRENT_TIMESTAMP)
AND ST_NM IS NOT NULL
GROUP BY ST_NM;
但是得到了语法错误:
SQL Editor: Encountered "WITH params AS. Was expecting one of: "alter" ... "call" ... "create" ... "drop" ... "dump" ... "logging" ... "replace" ...
"replication" ... "restart" ... "restore" ... ";" ... "dynamic" ... "explain" ... "lock" ... "locking" ... "with" ... "with" "recursive" ... "with" "(" ... "(" ... "validtime" ...
"current" ... "transactiontime" ... "nontemporal" ... "as" ... "select" ... "sel" ... "retrieve" ... "ret" ... "cd" ... "modify" ... "give" ... "rename" ... "cm" ... "delete" ...
"del" ... "release" ... "revalidate" ... "rollback" ... "rollforward" ... "checkpoint" ... "insert" ... "ins" ... "update" ... "upd" ... "merge" ... "set" ... "commit" ...
"abort" ... "begin" ... "bt" ... "end" ... "et" ... "ct" ... "grant" ... "cv" ... "revoke" ... "help" ... "show" ... "mload" ... "execute" ... "exec" ... "comment" ... "collect" ...
"database" ... "echo" ... "ss" ... "diagnostic" ... "initiate" ...
我尝试了一些随机的事情,例如删除“WITH”并将“AS”替换为“IS”,我知道这不是很好的努力。有人可以建议我应该是这种类型的查询的正确语法,因为我认为我缺少与语法相关的东西。 更新:我想做的是(忘了添加原帖)
UPDATE:对于没有更多的假脱机空间错误,我使用了建议来使用内部联接,并且查询开始按预期获取结果。所以我现在使用的最终查询是:
WITH TEMP_TAB (w0s,w0e,w4s,w4e) AS
(
SELECT
TO_TIMESTAMP(TO_CHAR(CURRENT_TIMESTAMP, 'yyyy-mm-dd hh24'),'yyyy-mm-dd hh24') AS w0s
,CURRENT_TIMESTAMP AS w0e
,CURRENT_DATE -29 AS w4s
,CURRENT_DATE -1 AS w4e
)
SELECT ST_NM
,SUM(CASE WHEN CRT_DTTM>=w0s AND CRT_DTTM<w0e THEN 1 ELSE 0 END) AS Call_Volume_This_hr
,SUM(CASE WHEN CRT_DTTM>=w4s AND CRT_DTTM<w4e THEN 1 ELSE 0 END)/28.0 AS Avg_Call_Volume_Past_4Weeks
,CASE WHEN Call_Volume_This_hr < Avg_Call_Volume_Past_4Weeks THEN 0
ELSE Call_Volume_This_hr -Avg_Call_Volume_Past_4Weeks
END AS VARIANCE
,100 * VARIANCE / Avg_Call_Volume_Past_4Weeks AS Percentage_Variance
FROM XDW_V.INB_CALL_NRT_RPT INNER JOIN TEMP_TAB
ON 1=1
WHERE CRT_DTTM>=w4s
AND EXTRACT(HOUR FROM CRT_DTTM)=EXTRACT( HOUR FROM CURRENT_TIMESTAMP)
AND ST_NM IS NOT NULL
GROUP BY ST_NM;