0

avg() 集計関数の条件に基づいて select stmt の結果が必要なクエリを書いています。

しかし、mysqlではエラーコードとしてエラーが発生しています:1111、グループ関数の無効な使用

select 
if( avg(FLD_TKTS) > 1 and avg(FLD_TKTS) <= 2  ,  '1-2' , 0) as fld_avg_1_2,
if( avg(FLD_TKTS) > 3 and avg(FLD_TKTS) <= 4  ,  '3-4' , 0) as fld_avg_3_4
from tbl
group by region;

これを行う他の方法はありますか?

4

3 に答える 3

2

クエリ:

select 
CASE WHEN avg(FLD_TKTS) > 1 and avg(FLD_TKTS) <= 2 THEN '1-2'
     ELSE '0' END as fld_avg_1_2,
CASE WHEN avg(FLD_TKTS) > 3 and avg(FLD_TKTS) <= 4 THEN '3-4' 
     ELSE '0' END as fld_avg_3_4
from tbl
group by region
于 2013-06-25T05:52:00.260 に答える
1
select
  region,
  if(fld_avg > 1 and fld_avg <= 2, '1-2', 0) as fld_avg_1_2,
  if(fld_avg > 3 and fld_avg <= 4, '3-4', 0) as fld_avg_3_4
from
(
  select 
    region,
    avg(FLD_TKTS) fld_avg
  from 
    tbl
  group by 
    region
) a;
于 2013-06-25T05:53:00.037 に答える
0

サブクエリが良い解決策になると思います:

SELECT 
    IF( avg_fld_tkts > 1 and avg_fld_tkts <= 2  ,  '1-2' , 0) as fld_avg_1_2,
    IF( avg_fld_tkts > 3 and avg_fld_tkts <= 4  ,  '3-4' , 0) as fld_avg_3_4
(
    SELECT avg(FLD_TKTS) AS avg_fld_tkts
    from tbl
    group by region
) t1
于 2013-06-25T05:53:22.733 に答える