#mysql
#mysql
Вопрос:
Как мне ВЫПОЛНИТЬ ЛЕВОЕ ПРИСОЕДИНЕНИЕ к среднему значению по этому запросу?
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
И различные другие слабые попытки безрезультатны.
Комментарии:
1. Возможно, я ошибаюсь (здесь уже поздно), но я не вижу поля id в определении t…
Ответ №1:
Трудно сказать точно, не зная больше о том, что такое «table2» и как это связано с «таблицей», но моей первой мыслью было бы выполнить join внутри вашего подзапроса, а не за его пределами:
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