1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

  2. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

(Postgres) How to set value based on row's value

Discussão em 'Outras Linguagens' iniciado por Stack, Janeiro 11, 2021.

  1. Stack

    Stack Membro Participativo

    For example, I have this table:

    Table_A

    id | classification | key | value | date_time
    ------+----------------+------------+--------------+--------------------
    1 | Num_A | Odd | 7 | 01-11-2021 01:00
    2 | Num_A | Status | Valid | 01-11-2021 01:00
    3 | Num_A | Odd | 9 | 01-11-2021 02:00
    4 | Num_A | Status | Valid | 01-11-2021 02:00
    5 | Num_B | Odd | 11 | 01-11-2021 02:00
    6 | Num_B | Status | Invalid | 01-11-2021 02:00
    7 | Num_C | Even | 10 | 01-11-2021 03:00
    8 | Num_C | Status | Valid | 01-11-2021 03:00


    My goal is to create a query that will result like this:

    id | classification | key | value | date_time
    ------+----------------+------------+--------------+--------------------
    1 | Num_A | Odd | Valid | 01-11-2021 01:00
    3 | Num_A | Odd | Valid | 01-11-2021 02:00
    5 | Num_B | Odd | | 01-11-2021 02:00
    7 | Num_C | Even | Valid | 01-11-2021 03:00


    Basically, this is just mapping of the key and value. If value is 'Invalid', display as blank (can be achieved using CASE statements).

    I have tried the several SQLs however I still cannot achieve the desired output. How can I achieve this?

    By the way, I am using PostgreSQL 11.

    Thank you in advance.

    Continue reading...

Compartilhe esta Página