Problem sorting sql with orderby Date in postgresql

Asked

Viewed 789 times

1

I have the following query that results in the union of the total sum of an account and the installments

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 Union, but when I use Union they end up being disordered, as for example 03/2018 comes in front of 04/2017. i am sending the date to to_char to can put in the format I want and when I am ordering I use it in date format, the error is occasioned by the union, so as I do to be ordered the whole date set of union?

Output from the query

 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"
  • You will need to remove the to_char of the subconsultation that is in the from. In the current form the order by is being performed as if datavencimento is a text and not a date.

3 answers

1


This will work:

select 
    sum(total) as total, 
    datavencimento
from  (
    select 
        sum (con_valoraserpago) as total,
        to_char(con_datavencimento,'mm/yyyy') as datavencimento,
        **date_trunc('month', con_datavencimento) as data_order**
    from conta  
    where con_formapagamento='V'  
    group by datavencimento, **data_order**  
    union
    select 
        sum (par_valorparcela) as total,
        to_char(par_datavencimento,'mm/yyyy') as datavencimento,
        **date_trunc('month', par_datavencimento) as data_order**
    from parcela
    where par_estorno <= 0  
    group by datavencimento, **data_order**
) AS A 
group by datavencimento, **data_order**
order by **data_order** ASC NULLS FIRST;

See the test here!

  • Look at what appears 'ERROR: column "account.con_expiration" must appear in the GROUP BY clause or be used in an Aggregate Function LINE 9: con_dataexpiration as data_order ***********Error ****************#Xa; ERROR: column column "account.con_expiration" must appear in the GROUP BY clause or be used in an Aggregate Function SQL state: 42803' apparently I can’t use data_order as con_expiration, it gives me this error

  • Ahh failed to include the new column in the Order By <code>group by dataexpiration, data_order </code>

  • group by date, data_order

  • I had put this group by data _order too, but even then it didn’t work, the same error occurs ... Does the error consist of from? internal group by before Union in the case

  • @Brunoasimilar to that! I also forgot about this internal order by

  • @Brunoasimilitudesilva I just edited the answer and includes a test!

  • 1

    Wow really now it worked, thank you so much for your patience and for your help!

Show 2 more comments

0

Try it like this:

  select sum(total) as total, datavencimento 
        from  
        (
        SELECT sum(con_valoraserpago) as total,
            to_char(con_datavencimento,'mm/yyyy') as datavencimento ,
            to_char(con_datavencimento, "yyyy/mm") as data_order
        FROM conta  where con_formapagamento='V'  
        GROUP BY datavencimento  
        UNION
        SELECT sum(par_valorparcela) as total, 
                to_char(par_datavencimento,'mm/yyyy') as datavencimento ,
                to_char(par_datavencimento, 'yyyy/mm') as data_order
        FROM parcela  
        WHERE par_estorno <= 0  
        GROUP BY datavencimento
   ) 
   AS A group by datavencimento     
   order by data_order ASC NULLS FIRST
  • I ended up with the following error ERROR: column "yyyy/mm" does not exist LINE 6: to_char(con_datamaturity, "yyyy/mm") as data_o... ***********Error *************#Xa; ERROR: column "yyyy/mm" does not exist SQL state: 42703

0

Try So

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 TO_DATE(datavencimento,'mm/yyyy') ASC NULLS FIRST;

This way it will convert date from text to date and will sort taking into account month and year.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.