Teradata查询中的语法错误:遇到“WITH

时间:2015-08-20 13:19:19

标签: teradata

我正在尝试使用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”,我知道这不是很好的努力。有人可以建议我应该是这种类型的查询的正确语法,因为我认为我缺少与语法相关的东西。 更新:我想做的是(忘了添加原帖)

  1. 如果说CURRENT_TIMESTAMP 2015-08-20 16:27:42.987那么 获取计数(*)按Create_time介于其中的每个状态分组 2015-08-20 16:00:00.000至2015-08-20 16:27:42.987。
  2. 二 步骤是查找过去7天按州分组的计数() (8月13日至8月19日)在同一个1小时的窗口16:00:00.000到 17:00:00.000并计算过去7天的总数()和平均值 count(*)(除以7)。
  3. 第三步是计数() 今天(在步骤1中计算)大于平均数() 然后找到差异并显示在一列(比如Variance)和 也可以使用(方差/平均值)计算百分比偏差 count(*))* 100并在列中显示百分比。
  4. 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;
    

0 个答案:

没有答案