在Teradata上运行查询时出错 - 选定的非聚合值必须是关联组的一部分

时间:2016-07-08 08:27:05

标签: teradata

f

有人可以指出错过了哪个参数来找到这个错误的解决方案,其次是哪个Group By语句产生了这个错误。

1 个答案:

答案 0 :(得分:1)

有两个问题,请参阅我的内联注释(格式化surce代码以使其可读)

SELECT 
   Case 
      When msa_high>=50 
         AND msa_high<=60 
      then 'low'
      When msa_high>60 
         AND msa_high>=70 
      then 'medium'
      When msa_high>70 
      then 'high' 
   end as edu_lv , 
   Sum(rev)/Sum(S_days) AS avg_sales
from 
   store_msa 
left join 
  ( Select
      (EXTRACT(YEAR FROM t.saledate)|| '-'||
         EXTRACT(MONTH FROM t.saledate) || '-'||
         t.store ) AS mys, 
      COUNT(DISTINCT t.saledate) AS S_days, 
      sum(t.amt) AS rev
   From 
      trnsact t
   Where (t.saledate)<'2005-08-01' 
      AND t.stype='P' 
      AND mys IN
       ( SELECT
            (EXTRACT(YEAR FROM t.saledate)|| '-'||
              EXTRACT(MONTH FROM t.saledate) || '-'||
              t.store) AS mys
         FROM 
            trnsact t
         HAVING 
            COUNT(DISTINCT t.saledate)>20
         GROUP BY 
            mys, 
            t.store 
       ) 
    -- ***** missing GROUP BY, probably mys, store again? *****
  ) AS rev ON store_msa.store=rev.store
Group By 
   edu_lv, 
   avg_sales  -- ***** remove this, can't group by an aggregated column *****
Order By 
   avg_sales;