pg.ddx.io  pgsql-pt-geral@postgresql.org mailing list archive  
help / color / mirror / Atom feed
UPDATE COM SELECT
6+ messages / 3 participants
[nested] [flat]

* UPDATE COM SELECT
@ 2021-03-31 20:56  Ranier Vilela <ranier.vf@gmail.com>
  0 siblings, 2 replies; 6+ messages in thread

From: Ranier Vilela @ 2021-03-31 20:56 UTC (permalink / raw)
  To: Comunidade PostgreSQL Brasileira <pgsql-pt-geral@lists.postgresql.org>

Olá,

Alguém sabe dizer porque essa "query" não funciona, ou sabe
de alguma maneira melhor de fazer update com select.

UPDATE tbContas AS CA SET
Debitos = (CASE WHEN ContaID = sq.ContaDebID THEN Debitos + sq.Valor ELSE
Debitos END),
Creditos = (CASE WHEN ContaID = sq.ContaCrdID THEN Creditos + sq.Valor ELSE
Creditos END)
FROM (
   SELECT L.ContaDebID,
     L.ContaCrdID,
     L.Valor
   FROM   tbLancamentos AS L
   INNER JOIN tbContas AS CA1 ON CA1.ContaID = L.ContaDebID
   INNER JOIN tbContas AS CA2 ON CA2.ContaID = L.ContaCrdID
   ) AS sq
WHERE (CA.ContaID = sq.ContaDebID OR CA.ContaID = sq.ContaCrdID)

Obrigado.

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: UPDATE COM SELECT
@ 2021-03-31 22:20  Leandro Guimarães Faria Corcete DUTRA <l@dutras.org>
  parent: Ranier Vilela <ranier.vf@gmail.com>
  1 sibling, 1 reply; 6+ messages in thread

From: Leandro Guimarães Faria Corcete DUTRA @ 2021-03-31 22:20 UTC (permalink / raw)
  To: Ranier Vilela <ranier.vf@gmail.com>; +Cc: Comunidade PostgreSQL Brasileira <pgsql-pt-geral@lists.postgresql.org>

Le mer. 31 mars 2021 à 17:56, Ranier Vilela <ranier.vf@gmail.com> a 
écrit :
> Alguém sabe dizer porque essa "query" não funciona

Precisaríamos pelo menos da estrutura das tabelas, e do resultado 
obtido (mensagem de erro, dados iesperados…)


-- 
+55 (61)  3546 7191 res.               xmpp:leandrod@jabber.org
+55 (61) 99302 2691 cel.              matrix:user/ld:matrix.org
+55 (61)  3216 3613 Chamber of deputies     mailto:l@dutras.org
Brasília, DF, Brazil GMT−3          https://useplaintext.email/







^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: UPDATE COM SELECT
@ 2021-03-31 23:06  Ranier Vilela <ranier.vf@gmail.com>
  parent: Leandro Guimarães Faria Corcete DUTRA <l@dutras.org>
  0 siblings, 1 reply; 6+ messages in thread

From: Ranier Vilela @ 2021-03-31 23:06 UTC (permalink / raw)
  To: Leandro Guimarães Faria Corcete DUTRA <l@dutras.org>; +Cc: Comunidade PostgreSQL Brasileira <pgsql-pt-geral@lists.postgresql.org>

Em qua., 31 de mar. de 2021 às 19:20, Leandro Guimarães Faria Corcete DUTRA
<l@dutras.org> escreveu:

> Le mer. 31 mars 2021 à 17:56, Ranier Vilela <ranier.vf@gmail.com> a
> écrit :
> > Alguém sabe dizer porque essa "query" não funciona
>
> Precisaríamos pelo menos da estrutura das tabelas, e do resultado
> obtido (mensagem de erro, dados iesperados…)
>
Olá, obrigado por responder.
A "query" funciona parcialmente.

SELECT L.ContaDebID,
     L.ContaCrdID,
     L.Valor
   FROM   tbLancamentos AS L
   INNER JOIN tbContas AS CA1 ON CA1.ContaID = L.ContaDebID
   INNER JOIN tbContas AS CA2 ON CA2.ContaID = L.ContaCrdID

Traz os lançamentos de partida dobrada, que por exemplo são:
ContaDebID, ContaCrdID, Valor
6                    9                   100
9                    6                   120

Então as contas deveriam ficar assim:
ContaID Debitos Creditos
6            220       220
9            220       220

Mas estão ficando assim:
ContaID Debitos Creditos
6            100       100
9            120       120

Obrigado.

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: UPDATE COM SELECT
@ 2021-03-31 23:08  Leandro Guimarães Faria Corcete DUTRA <l@dutras.org>
  parent: Ranier Vilela <ranier.vf@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Leandro Guimarães Faria Corcete DUTRA @ 2021-03-31 23:08 UTC (permalink / raw)
  To: Ranier Vilela <ranier.vf@gmail.com>; +Cc: Comunidade PostgreSQL Brasileira <pgsql-pt-geral@lists.postgresql.org>

Le mer. 31 mars 2021 à 20:06, Ranier Vilela <ranier.vf@gmail.com> a 
écrit :
> A "query" funciona parcialmente.

Estrutura das tabelas?


-- 
+55 (61)  3546 7191 res.               xmpp:leandrod@jabber.org
+55 (61) 99302 2691 cel.              matrix:user/ld:matrix.org
+55 (61)  3216 3613 Chamber of deputies     mailto:l@dutras.org
Brasília, DF, Brazil GMT−3          https://useplaintext.email/







^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: UPDATE COM SELECT
@ 2021-03-31 23:33  Ranier Vilela <ranier.vf@gmail.com>
  parent: Leandro Guimarães Faria Corcete DUTRA <l@dutras.org>
  0 siblings, 0 replies; 6+ messages in thread

From: Ranier Vilela @ 2021-03-31 23:33 UTC (permalink / raw)
  To: Leandro Guimarães Faria Corcete DUTRA <l@dutras.org>; +Cc: Comunidade PostgreSQL Brasileira <pgsql-pt-geral@lists.postgresql.org>

Em qua., 31 de mar. de 2021 às 20:08, Leandro Guimarães Faria Corcete DUTRA
<l@dutras.org> escreveu:

> Le mer. 31 mars 2021 à 20:06, Ranier Vilela <ranier.vf@gmail.com> a
> écrit :
> > A "query" funciona parcialmente.
>
> Estrutura das tabelas?
>

Segue um exemplo contido das estruturas:

DROP TABLE IF EXISTS tbContas;
DROP TABLE IF EXISTS tbLancamentos;

CREATE TABLE tbContas (
 ContaID INT  PRIMARY KEY,
 Debitos Numeric(18,2),
 Creditos Numeric(18,2));

CREATE TABLE tbLancamentos (
 ContaDebID INT,
 ContaCrdID INT,
 Valor Numeric(18,2));

INSERT INTO tbContas (ContaID) Values(6);
INSERT INTO tbContas (ContaID) Values(9);
INSERT INTO tbLancamentos (ContaDebID, ContaCrdID, Valor) Values(6, 9, 100);
INSERT INTO tbLancamentos (ContaDebID, ContaCrdID, Valor) Values(9, 6,
120);

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: UPDATE COM SELECT
@ 2021-04-01 01:46  Arthur Nascimento <tureba@gmail.com>
  parent: Ranier Vilela <ranier.vf@gmail.com>
  1 sibling, 0 replies; 6+ messages in thread

From: Arthur Nascimento @ 2021-04-01 01:46 UTC (permalink / raw)
  To: Ranier Vilela <ranier.vf@gmail.com>; +Cc: Comunidade PostgreSQL Brasileira <pgsql-pt-geral@lists.postgresql.org>

On Wed, 31 Mar 2021 at 17:57, Ranier Vilela <ranier.vf@gmail.com> wrote:

> Alguém sabe dizer porque essa "query" não funciona, ou sabe
> de alguma maneira melhor de fazer update com select.
>
> UPDATE tbContas AS CA SET
> Debitos = (CASE WHEN ContaID = sq.ContaDebID THEN Debitos + sq.Valor ELSE
> Debitos END),
> Creditos = (CASE WHEN ContaID = sq.ContaCrdID THEN Creditos + sq.Valor
> ELSE Creditos END)
> FROM (
>    SELECT L.ContaDebID,
>      L.ContaCrdID,
>      L.Valor
>    FROM   tbLancamentos AS L
>    INNER JOIN tbContas AS CA1 ON CA1.ContaID = L.ContaDebID
>    INNER JOIN tbContas AS CA2 ON CA2.ContaID = L.ContaCrdID
>    ) AS sq
> WHERE (CA.ContaID = sq.ContaDebID OR CA.ContaID = sq.ContaCrdID)
>

O update não pode alterar mais de uma vez cada linha. E claramente não
existe razão para acreditar que teria apenas uma linha em tbLancamentos
para cada uma de tbContas.

Se quiser guardar em tbContas o _total_ de mudanças desses lançamentos,
execute um select que faça a totalização e então atualize com ela. Para
ficar mais claro em dois comandos (não testado):

with
debitos_totais as (select ContaDebID, sum(Valor) as total from
tbLancamentos group by 1)
update tbContas set Debitos = Debitos + debitos_totais.total from
debitos_totais where tbContas.ContaID = debitos_totais.ContaDebID;

with
creditos_totais as (select ContaCrdID, sum(Valor) as total from
tbLancamentos group by 1)
update tbContas set Creditos = Creditos + creditos_totais.total from
creditos_totais where tbContas.ContaID = creditos_totais.ContaCrdID;


-- 

Tureba - Arthur Nascimento

^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2021-04-01 01:46 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-03-31 20:56 UPDATE COM SELECT Ranier Vilela <ranier.vf@gmail.com>
2021-03-31 22:20 ` Leandro Guimarães Faria Corcete DUTRA <l@dutras.org>
2021-03-31 23:06   ` Ranier Vilela <ranier.vf@gmail.com>
2021-03-31 23:08     ` Leandro Guimarães Faria Corcete DUTRA <l@dutras.org>
2021-03-31 23:33       ` Ranier Vilela <ranier.vf@gmail.com>
2021-04-01 01:46 ` Arthur Nascimento <tureba@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox