1
I need the data you have tr_color = success are ordered in a descending manner DESC and those who do not have it, that the column is empty, be ordered in an Ascending manner ASC. 
I’m assembling the query this way.
SELECT mc.competencia,
CASE
   WHEN (COALESCE(mc.valor_pago, 0.0) = COALESCE(mc.valor_devido, 0.0)) THEN 'success'
   WHEN (COALESCE(mc.valor_pago, 0.0) = 0) THEN ''
   WHEN (COALESCE(mc.valor_pago, 0.0) <> COALESCE(mc.valor_devido, 0.0)) THEN 'danger'
END AS tr_color
FROM movimento_cobranca AS mc
WHERE mc.matricula = 2035
ORDER BY tr_color
And this returning me the moment the following result.
Competencia |  tr_color
-----------------------
0419              ""
0419              ""
0419              ""
0519              ""
0619              ""
0719              ""
0819              ""
0919              ""
1019              ""
1119              ""
1219              ""
0419              ""
0319              success
0118              success
0319              success
0219              success
0119              success
0218              success
0318              success
0418              success
0518              success
0618              success
0718              success
0818              success
0918              success
1018              success
1118              success
1218              success
I need to return so that those who possess Success stay in the following order.
0319 - 0219 - 0119 - 1218 - 1118 ... 
And those who don’t Success remain the same.
EDIT SOLUTION
SELECT * FROM
  (
    SELECT mc.competencia,
           CASE
             WHEN (COALESCE(mc.valor_pago, 0.0) = COALESCE(mc.valor_devido, 0.0)) THEN 'success'
             WHEN (COALESCE(mc.valor_pago, 0.0) = 0) THEN ''
             WHEN (COALESCE(mc.valor_pago, 0.0) <> COALESCE(mc.valor_devido, 0.0)) THEN 'danger'
             END AS tr_color
    FROM movimento_cobranca AS mc
    WHERE mc.matricula = 2035
      AND COALESCE(mc.valor_pago, 0.0) > 0.0
    ORDER BY data_vencimento DESC
  ) AS p
UNION ALL
(
  SELECT mc.competencia,
         CASE
           WHEN (COALESCE(mc.valor_pago, 0.0) = COALESCE(mc.valor_devido, 0.0)) THEN 'success'
           WHEN (COALESCE(mc.valor_pago, 0.0) = 0) THEN ''
           WHEN (COALESCE(mc.valor_pago, 0.0) <> COALESCE(mc.valor_devido, 0.0)) THEN 'danger'
           END AS tr_color
  FROM movimento_cobranca AS mc
  WHERE mc.matricula = 2035
    AND COALESCE(mc.valor_pago, 0.0) = 0.0
  ORDER BY data_vencimento
)
It’s the same field that they use @Rbz. Oh my doubt on how to do hahah
– Guilherme Rigotti
SE talcoisa USE campo1 OU -campo2(note the sign of - ) - With case when or equivalent in pgsql (in Mysql you have IF, in T-SQL you have IIF in 2012)– Bacco
You can select from select and sort by those fields as suggested by @Rbz
– Reginaldo Rigo
I had tried to make one
UNIONbut without success, perhaps because of my lack of knowledge in SQL.– Guilherme Rigotti
@Guilhermerigotti Union tb serves well, if you can separate the 2 sets. SELECT fields WHERE tr_color = Success ORDER BY field1 ASC UNION SELECT fields WHERE tr_color <> Success ORDER BY field2 DESC (Set to your DB)
– Bacco
How would the
CASE?CASE WHEN tr_color <> 'success' THEN ....what I would be in the THEN?– Guilherme Rigotti
@Bacco I can’t do 2 ORDER BY no
Postgresusing UNION.– Guilherme Rigotti
Try to separate the select with (SELECT ...) UNION (SELECT ...) - Rarely use pgsql, I don’t like, to tell the truth, I barely remember the specifics. I just really wanted to say something beforehand until the official answer comes
– Bacco
@Bacco I understand, because it is pgsql is really boring.
– Guilherme Rigotti