How to fix "[Teradata][ODBC Teradata Driver][Teradata Database] Syntax error: ORDER BY is not allowed in subqueries." in tableau query

How to fix "[Teradata][ODBC Teradata Driver][Teradata Database] Syntax error: ORDER BY is not allowed in subqueries." in tableau query

我正在创建一个查询,用于从将在 tableau 中使用的 teradata 中提取数据,我需要修复查询的哪一部分?

这个查询我在 Teradata sql 助手中尝试过,它有效,但是当我移动到画面时,它总是以错误 ORDER BY is not allowed in subquerie

teradata sql 查询下方:

    Select  
    ChannelType,Group,
    LevelType,
    EXTRACT(YEAR From CDate) as KYear, 
    EXTRACT(MOnth From CDate) as KMonth, 

    Case 
    when Position('Live' IN modeType) >0 then 'L'
    else 'S'
    end as LS,

    Sum(watch) as Watches,
    Sum(amount) as amounts
    zeroifnull(amounts)/zeroifnull(Watches,)as result,

    CASE 
    WHEN result >0 then 'WIN'
    ELSE 'LOSE'
    END AS WL Class

    From Mart_wodo.data where
    CDate between date'2018-01-01' and date'2018-12-31'  and
    Group in('L1','L2','L3','L4','L5') and
    LevelType NOT IN('0') and 
    Channeltype in('TV','TY,'IG')

    Group By Channeltype , Group, LevelType, KYear, KMonth,LS

    Order BY  KYear, KMonth,Channeltype, Group,LS asc

我的猜测是 Tableau 将您的查询包装在外部 SELECT 中,这就是您收到 ORDER BY not allowed 错误消息的原因,但它在 SQL 中按原样工作助手。

如果是这种情况,您需要删除 ORDER BY 子句并查看是否可以通过 Tableau 指定最终顺序。或者可能将您的 SQL 包装在一个视图中并通过 Tableau 访问该视图:link

作为一个潜在的 hack(它可能不会起作用),您可以尝试重新排序 GROUP BY 列或添加一个带有 ORDER BY 子句的虚拟 window 函数,并且看看有没有效果:

Select  
  ChannelType,Group,
  ...
  ROW_NUMBER() OVER(ORDER BY KYear, KMonth, Channeltype, Group, LS ASC) AS RowNum

From Mart_wodo.data where
CDate between date'2018-01-01' and date'2018-12-31'  and
Group in('L1','L2','L3','L4','L5') and
LevelType NOT IN('0') and 
Channeltype in('TV','TY,'IG')

Group By KYear, KMonth, Channeltype, Group, LS, LevelType