Teradata - 这个查询有什么问题? “选定的非聚合值必须是关联组的一部分。”

时间:2018-02-05 08:10:22

标签: sql aggregate teradata

我目前正在使用teradata处理sql问题,但我不断收到错误消息,虽然我找不到任何问题。 这是我的疑问:

SELECT TOP 10 d.deptdesc, tt.store, SUM(tt.amt11) AS sum11, SUM(tt.amt12) AS sum12,
        SUM(tt.num11) AS num11, SUM(tt.num12) AS num12,
        sum11/num11 AS avg11, sum12/num12 AS avg12,
        (avg12-avg11)/avg11 * 100 AS increase
FROM(SELECT store, 
        SUM(CASE monthId WHEN 11 THEN total_revenue END) AS amt11,
        SUM(CASE monthId WHEN 12 THEN total_revenue END) AS amt12,
        SUM(CASE monthId WHEN 11 THEN date_num END) AS num11,
        SUM(CASE monthId WHEN 12 THEN date_num END) AS num12
    FROM(SELECT EXTRACT(MONTH FROM saledate) AS monthId,
                EXTRACT(YEAR FROM saledate) AS yearId,
                store,
                SUM(amt) AS total_revenue,
                COUNT(DISTINCT saledate) AS date_num       
            FROM trnsact
            WHERE stype = 'P' AND NOT(monthId = '8' AND yearId = '2005') 
            GROUP BY monthId, yearId, store
            HAVING date_num >= 20) t
    GROUP BY store) tt 
INNER JOIN (SELECT sku, store FROM trnsact) ttt ON tt.store = ttt.store 
INNER JOIN skuinfo sku ON ttt.sku = sku.sku
INNER JOIN deptinfo d ON sku.dept = d.dept
GROUP BY d.deptdesc, tt.store
HAVING sum11 > 1000 AND sum12 > 1000
ORDER BY increase DESC;

Teradata发出这样的信息

  

错误代码 - 3504
  错误消息 - [Teradata数据库] [TeraJDBC 15.10.00.05] [错误3504] [SQLState HY000]选定的非聚合值必须是关联组的一部分。

你可以给我任何建议吗? 提前谢谢!

2 个答案:

答案 0 :(得分:0)

解析器检查您为聚合但是的别名的非聚合列 您无法在select中直接使用select列的别名,因此您应该使用:

SELECT TOP 10 
  d.deptdesc
  , tt.store
  , SUM(tt.amt11) AS sum11
  , SUM(tt.amt12) AS sum12
  , SUM(tt.num11) AS num11
  , SUM(tt.num12) AS num12
  , SUM(tt.amt11)/SUM(tt.num11) AS avg11
  , SUM(tt.amt12)/SUM(tt.num12) AS avg12
  , (SUM(tt.amt12)/SUM(tt.num12)-SUM(tt.amt11)/SUM(tt.num11))/SUM(tt.amt11)/SUM(tt.num11) * 100 AS increase
  ....

答案 1 :(得分:0)

使用此查询:

SELECT TOP 10  deptdesc, store, sum11, sum12, num11, num12,
            sum11/num11 AS avg11, sum12/num12 AS avg12,
            ((sum12/num12)-(sum11/num11))/(sum11/num11)* 100 AS increase FROM
(
    SELECT d.deptdesc, tt.store, SUM(tt.amt11) AS sum11, SUM(tt.amt12) AS sum12,
            SUM(tt.num11) AS num11, SUM(tt.num12) AS num12
    FROM(SELECT store, 
            SUM(CASE monthId WHEN 11 THEN total_revenue END) AS amt11,
            SUM(CASE monthId WHEN 12 THEN total_revenue END) AS amt12,
            SUM(CASE monthId WHEN 11 THEN date_num END) AS num11,
            SUM(CASE monthId WHEN 12 THEN date_num END) AS num12
        FROM(SELECT EXTRACT(MONTH FROM saledate) AS monthId,
                    EXTRACT(YEAR FROM saledate) AS yearId,
                    store,
                    SUM(amt) AS total_revenue,
                    COUNT(DISTINCT saledate) AS date_num       
                FROM trnsact
                WHERE stype = 'P' AND NOT(monthId = '8' AND yearId = '2005') 
                GROUP BY monthId, yearId, store
                HAVING date_num >= 20) t
        GROUP BY store) tt 
    INNER JOIN (SELECT sku, store FROM trnsact) ttt ON tt.store = ttt.store 
    INNER JOIN skuinfo sku ON ttt.sku = sku.sku
    INNER JOIN deptinfo d ON sku.dept = d.dept
    GROUP BY d.deptdesc, tt.store
) AS T
    GROUP BY deptdesc, tt.store, sum11, sum12, num11, num12
    HAVING sum11 > 1000 AND sum12 > 1000
    ORDER BY increase DESC;