Return last result (updated by date) of each attribute

Asked

Viewed 34 times

0

I have a database with a table called config. In it I have the columns atributo, valor and data:

atributo              valor       data_atualizacao

juros_composto     |    5      |    2019-01-25
juros_fixo         |    6      |    2019-01-27
juros_fracionario  |    9      |    2019-01-28
juros_mediador     |    4      |    2019-01-30
juros_composto     |    12     |    2019-01-31
juros_fixo         |    14     |    2019-02-01
juros_fracionario  |    16     |    2019-02-02
juros_mediador     |    18     |    2019-02-03

I need to return the last attributes along with the values grouped by the update date, so the result should appear like this:

atributo                valor

juros_composto     |    12     |
juros_fixo         |    14     |
juros_fracionario  |    16     |
juros_mediador     |    18     |

I made a code like this:

SELECT valor MAX(data_atualizacao) FROM config GROUP BY atributo;

It returns the attributes without repeating (right) but the column 'VALUE' returns with the wrong data.

  • In case you want the last values registered in the Bank system ?

  • In the precise case of the last attribute values (without repeating) sorted by date. So for example, I have 4 types of attributes, I need to return the value of these attributes but only the most recent ones without repeating.

  • I’ll send an answer with SQL see if it works,

  • OK.You can leave!

1 answer

1


The best way I can think of you doing that is to do a second select within the where to determine the last date.

SELECT 
    atributo, valor, data_atualizacao
FROM
    config
WHERE
    data_atualizacao = (SELECT 
            MAX(config_sub.data_atualizacao)
        FROM
            config AS config_sub
        WHERE
            config.atributo = config_sub.atributo);

Test there and see if it works according to what you need.

Browser other questions tagged

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