how do i translate postgresql queries into mysql? Here is the actual request
with vw as ( select area_id, (case when to_char(period, 'yyyy')::INTEGER = %(year)s then to_char(period, 'mm')::INTEGER when to_char(period, 'yyyy')::INTEGER < %(year)s then 0 end) month_num, round(sum((case when type=1 then sum else 0 end))/1000000,1) consumption, round(sum((case when type=2 then sum else 0 end))/1000000,1) payment from clearing_main where (0 = %(abonent_type)s or abonent_type = %(abonent_type)s) and region_id = %(region_id)s and (0 = %(area_id)s or area_id = %(area_id)s) group by area_id, month_num ) select gs_month_num as id, (select sum(consumption)-sum(payment) from vw where vw.month_num < gs_month_num) begin_debt, (select sum(consumption) from vw where vw.month_num = gs_month_num) consumption, (select sum(payment) from vw where vw.month_num = gs_month_num) payment, (select sum(consumption)-sum(payment) from vw where vw.month_num = gs_month_num) debt_plus, (select sum(consumption)-sum(payment) from vw where vw.month_num <= gs_month_num) end_debt, (select (case when sum(consumption) > 0 then round(sum(payment)*100/sum(consumption), 1) else 0 end) from vw where vw.month_num = gs_month_num) percent from generate_series(1, extract('month' from now())::integer) gs_month_num order by gs_month_num; it also generates months from 1 to the desired month. I did something similar, but it only shows me one month.
SELECT g_month as id, (SELECT SUM(consumption)-SUM(payment) FROM y WHERE y.month < g_month) AS begin_debt, (SELECT SUM(payment) FROM y WHERE y.month = g_month) AS payment, (SELECT SUM(consumption)-SUM(payment) FROM y WHERE y.month <= g_month) AS end_debt, (SELECT (CASE WHEN SUM(consumption) > 0 THEN ROUND(SUM(payment)*100/SUM(consumption), 1) ELSE 0 END) FROM y WHERE y.month = g_month) AS percent FROM (SELECT MONTH(NOW()) AS g_month) g_month, (SELECT area_id, (CASE WHEN YEAR(period) = 2016 THEN MONTH(period) WHEN YEAR(period) < 2016 THEN 0 END) AS month, ROUND(SUM((CASE WHEN type = 1 THEN sum ELSE 0 END))/1000000, 1) AS consumption, ROUND(SUM((CASE WHEN type = 2 THEN sum ELSE 0 END))/1000000, 1) AS payment FROM main WHERE abonent_type = 1 AND region_id = 79 AND area_id = 7922 GROUP BY area_id, month) AS y ORDER BY g_month` Also in this (SELECT SUM(consumption)-SUM(payment) FROM y WHERE y.month < g_month) AS begin_debt is a call to the table y and it gives an error that the table was not found. I will be grateful.
(SELECT SUM(consumption)-SUM(payment) FROM y WHERE y.month < g_month) as begin_debtexpanded into something in the form ofSUM(IF(y.month < g_month, consumption-payment,0)). Because it seems that this is data from previous lines, it might be easier to get them back to variables in previous lines. And the subquery that calls from the sample list to table Y is basically impossible - Mike