-1
Hello guys I’m having a hard time not displaying the items of a duplicate column, in case the column C.CLI_ID, I need to show only the first result. I’ve tried several ways but SQL is not my specialty I’m already without ideas.
SELECT c.cli_id, C.cli_data_cadastro, CT.cont_id, CT.cont_data_vencimento, T.tituloid, T.dataven, T.datapag, T.valorpago,
CASE
WHEN T.valorpago <= '0' THEN 'Não Pago'
WHEN ct.cont_data_vencimento > FORMAT(GETDATE(), 'yyyy-MM-dd') AND T.valorpago > '0' THEN 'Pago'
WHEN ct.cont_data_vencimento < FORMAT(GETDATE(), 'yyyy-MM-dd') AND T.valorpago > '0' THEN 'Vencido'
END AS situacao
FROM cliente_rbdigital AS C, contrato_rbdigital AS CT
INNER JOIN titulo_rbdigital AS T ON T.contratoid=CT.cont_id
WHERE C.cli_id = CT.cont_cliente_id
ORDER BY C.cli_data_cadastro DESC, situacao DESC
10472 2020-07-31 11:58:44 39486 2020-08-30 39409 2020-08-03 NULL 0.00 Não Pago
10471 2020-07-31 09:57:22 39484 2021-01-27 39407 2020-08-03 2020-07-31 119.40 Pago
10470 2020-07-31 09:35:45 39483 2020-10-29 39406 2020-08-03 NULL 0.00 Não Pago
10469 2020-07-31 05:39:23 39488 2021-01-27 39411 2020-08-01 2020-07-31 119.40 Pago
10469 2020-07-31 05:39:23 39478 2021-01-27 39401 2020-08-03 NULL 0.00 Não Pago
10468 2020-07-30 21:48:26 39473 2020-08-29 39396 2020-08-02 2020-07-30 19.90 Pago
10467 2020-07-30 21:48:21 39472 2021-01-26 39395 2020-08-02 NULL 0.00 Não Pago
10466 2020-07-30 19:54:12 39467 2020-08-29 39391 2020-08-02 2020-07-30 19.90 Pago
10465 2020-07-30 16:54:29 39463 2020-08-29 39386 2020-08-02 NULL 0.00 Não Pago
10464 2020-07-30 09:18:43 39458 2021-07-30 39381 2020-08-02 2020-07-30 238.80 Pago
10463 2020-07-30 07:59:46 39459 2020-08-29 39382 2020-07-31 2020-07-30 9.90 Pago
You don’t want the results to be duplicated, correct?
– hh22
See: https://stackoverflow.com/questions/5808854/sql-server-equivalent-of-postgresql-distinct-on
– anonimo
And in the case of repeated lines for the same cli_id value, what is the criterion for defining which line will be displayed and which will be deleted?
– José Diz
What exactly do you want to do? It is not clear, because the lines are not duplicates. They are legitimate lines and according to the SQL you have assembled.
– Marcelo Shiniti Uchimura