I have the following MySQL query
select
keliones_lapas.Vairuot_Id,
MONTH(keliones_lapas.Data_darbo),
sum(keliones_lapas.uzdarbis) as Got,
coalesce(Suma, 0) Suma,
(sum(keliones_lapas.uzdarbis) - coalesce(Suma, 0)) Total
from keliones_lapas
left join (select Vairuotas, MONTH(Data_islaidu), sum(Suma) as Spend from
islaidos group by Vairuotas, MONTH(Data_islaidu))
islaidos on
keliones_lapas.Vairuot_Id=islaidos.Vairuotas and
MONTH(keliones_lapas.Data_darbo)=MONTH(islaidos.Data_islaidu)
group by keliones_lapas.Vairuot_Id, MONTH(keliones_lapas.Data_darbo), Suma
order by keliones_lapas.Vairuot_Id, MONTH(keliones_lapas.Data_darbo);
And i'm getting this error:
I know that this issue is known but i do not know how to fix it by my self. This is the cause of the issue which is solved
And i can provide with the fiddle : http://sqlfiddle.com/#!9/e76ef9/8
You just need to alias all of the calculated columns in the sub-query, and then you can reference those aliases in the ON
clause. Here is the SQL from your fiddle with the Data_islaidu and Suma columns aliased:
select keliones_lapas.Vairuot_Id, MONTH(keliones_lapas.Data_darbo), sum(keliones_lapas.uzdarbis) as Got, coalesce(Suma, 0) AS Spend, (sum(keliones_lapas.uzdarbis) - coalesce(Suma, 0)) Total
from keliones_lapas
left join (
select
Vairuotas,
MONTH(Data_islaidu) as Data_islaidu,
sum(Suma) as Suma
from islaidos
group by Vairuotas, MONTH(Data_islaidu)) islaidos
on keliones_lapas.Vairuot_Id = islaidos.Vairuotas
and MONTH(keliones_lapas.Data_darbo) = MONTH(islaidos.Data_islaidu)
group by keliones_lapas.Vairuot_Id, MONTH(keliones_lapas.Data_darbo), Suma
order by keliones_lapas.Vairuot_Id, MONTH(keliones_lapas.Data_darbo);