我有一张item_table这样的桌子:
item   age
--------------    
1      1 
1      6 
2      2    
我有另一个price_table这样的表:
item    pricetype    price
--------------------------    
1       O             5
1       P             6
1       V             7
2       O             8
2       P             9
2       V             10
所以,我想在两个表上进行内部连接。
select *
from item_table i
inner join price_table p
on ...
有一些条件on:
- 如果一个项目的平均年龄大于
3,那么我会:inner join price_table on pricetype = 'O' or pricetype = 'P' - 如果没有,那么我这样做:
inner join price_table on pricetype = 'O' or pricetype = 'P' or pricetype = 'V' 
所以有条件on有条件。
然后我写这样的查询:
select i.item, i.type, p.pricetype, p.price
from item_table i
inner join price_table p on i.item = p.item 
    and (avg(i.age) >= 3 and p.pricetype in ('O', 'P'))
        or (avg(i.age) < 3 and p.pricetype in ('O', 'P', 'V'))
给出的错误是:An aggregate cannot appear in an ON clause unless it is in a subquery contained in a HAVING clause or select list, and the column being aggregated is an outer reference.
我不能移动到avg,Having因为其他条件取决于avg.
如何编写选择查询?