如何将平均值加入此查询?
select t.range, count(*) as num
from
(select case
when price < 50000 then '0 - 49K'
when price >= 50000 and price < 100000 then '50 - 99K'
when price >= 100000 and price < 200000 then '100 - 199K'
...
as range,
price
from table) as t
group by range
我试过
select t.range, count(*) as num, avg(b.val)
from
(select case
when price < 50000 then '0 - 49K'
when price >= 50000 and price < 100000 then '50 - 99K'
when price >= 100000 and price < 200000 then '100 - 199K'
...
as range,
price
from table) as t
left join table2 b on b.id = t.id
group by range
其他各种微弱的尝试无济于事。
答案 0 :(得分:0)
很难说完全不知道“table2”是什么以及它与“table”的关系,但我的第一个想法是在子查询中而不是在它之外进行连接:
select t.range, count(*) as num, avg(t.val)
from
(select case
when price < 50000 then '0 - 49K'
when price >= 50000 and price < 100000 then '50 - 99K'
when price >= 100000 and price < 200000 then '100 - 199K'
...
as range,
t1.price,
b.val
from table t1
left join table2 b on b.id = t1.id
) as t
group by range