Using case to match fields

Asked

Viewed 29 times

2

I have the following structure

---------------------------------------
|DT_Ven  | VLR_CONTA|DT_QUI  | VLR_QUI|
|--------------------------------------
|30/07/20|     84,00|NULL    |   84,00|
|03/08/20|    100,00|NULL    |  100,00|
|04/08/20|   1505,96|04/08/20| 1505,96|
|06/08/20|     10,00|06/08/20|   10,00|
|07/08/20|     50,00|07/08/20|   50,00|
|01/10/20|    350,00|01/10/20|  350,00|
|20/10/20|    200,00|20/10/20|  200,00|
---------------------------------------

I want the field DT_QUI if he is Null bring just like the field DT_Ven like this below.

---------------------------------------
|DT_Ven  | VLR_CONTA|DT_QUI  | VLR_QUI|
|--------------------------------------
|30/07/20|     84,00|30/07/20|   84,00|
|03/08/20|    100,00|03/08/20|  100,00|
|04/08/20|   1505,96|04/08/20| 1505,96|
---------------------------------------

have tried sql so, more error.

SELECT
    CONTAS_PAGAR.DT_VENC,
    CONTAS_PAGAR.VLR_CONTA, 
  CASE
    WHEN CONTAS_PAGAR.DT_QUITACAO IS NULL
      THEN CONTAS_PAGAR.DT_QUITACAO = CONTAS_PAGAR.DT_VENC 
    ELSE CONTAS_PAGAR.DT_QUITACAO
  END AS DT_QUITACAO,
    CONTAS_PAGAR.VLR_QUITACAO
FROM
    CONTAS_PAGAR
  • 1

    You did THEN CONTAS_PAGAR.DT_QUITACAO = CONTAS_PAGAR.DT_VENC should only be THEN CONTAS_PAGAR.DT_VENC and in the end rename with AS (alias or nickname)

  • Its initial field structure is not equal to the columns of the SQL. In the first table you did not inform that there is the field SITUACAO. Could you edit your question.

  • 3

    In place of CASE you can use the COALESCE function: COALESCE(DT_QUIT, DT_VENC) AS DT_QUITACAO, she serves exactly for this.

1 answer

2


Browser other questions tagged

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