I have the following Query that results in joining the total sum of an account and the plots
select
sum(total) as total,
datavencimento
from (
select
sum (con_valoraserpago) as total,
to_char(con_datavencimento,'mm/yyyy') as datavencimento
from conta
where con_formapagamento='V'
group by datavencimento
union
select
sum (par_valorparcela) as total,
to_char(par_datavencimento,'mm/yyyy') as datavencimento
from parcela
where par_estorno <= 0
group by datavencimento
) AS A
group by datavencimento
order by datavencimento ASC NULLS FIRST;
If I run select by select they bring me sorted correctly by the union, but when I use the union they end up coming cluttered, as for example 03/2018 comes in front of 04/2017. I am sending the date to to_char so I can put in the format I want and when I order it I use it in date format, the error is caused by the union, so how do I do to be sorted the date all this union set?
Query output
total;datavencimento
200;"01/2017"
200;"02/2017"
200;"02/2018"
1534.24;"03/2017"
200;"03/2018"
450;"04/2017"
50;"05/2017"
650;"06/2017"
2879.8;"07/2017"
200;"08/2017"
200;"09/2017"
200;"10/2017"
200;"12/2017"