summ and update

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

summ and update

Post by sal21 »

SELECT T.CARTA, Sum(T.IMPORTO) AS SommaDiIMPORTO
FROM T INNER JOIN DB_CARTE ON T.CARTA = DB_CARTE.CARTA
GROUP BY T.CARTA

Now, i need to update the field IMPORTO in DB_CARTE with SommaDiIMPORTO, possible?

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: summ and update

Post by HansV »

You can't use a Totals query in an update query. Does this work?

Code: Select all

UPDATE DB_CARTE SET IMPORTO = DSum("IMPORTO", "T", "CARTA='" & CARTA & "'")
I have assumed that CARTA is a text field. If it is a number field, omit the single quotes '.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: summ and update

Post by sal21 »

HansV wrote:You can't use a Totals query in an update query. Does this work?

Code: Select all

UPDATE DB_CARTE SET IMPORTO = DSum("IMPORTO", "T", "CARTA='" & CARTA & "'")
I have assumed that CARTA is a text field. If it is a number field, omit the single quotes '.

Sorry me Hans but you just have posted a solution for this prob. :grin: :sad:
In other case tks.

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: summ and update

Post by sal21 »

HansV wrote:You can't use a Totals query in an update query. Does this work?

Code: Select all

UPDATE DB_CARTE SET IMPORTO = DSum("IMPORTO", "T", "CARTA='" & CARTA & "'")
I have assumed that CARTA is a text field. If it is a number field, omit the single quotes '.
Hans, sorry me but...
Is possible to count how many occurence are compressed in DSum and put the count value into the correspondent field NrTot?

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: summ and update

Post by HansV »

Perhaps

Code: Select all

UPDATE DB_CARTE SET IMPORTO = DSum("IMPORTO", "T", "CARTA='" & CARTA & "'"), NrTot = DCount("IMPORTO", "T", "CARTA='" & CARTA & "'")
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: summ and update

Post by sal21 »

HansV wrote:Perhaps

Code: Select all

UPDATE DB_CARTE SET IMPORTO = DSum("IMPORTO", "T", "CARTA='" & CARTA & "'"), NrTot = DCount("IMPORTO", "T", "CARTA='" & CARTA & "'")
work perfect!
tks :thankyou:

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: summ and update

Post by sal21 »

HansV wrote:Perhaps

Code: Select all

UPDATE DB_CARTE SET IMPORTO = DSum("IMPORTO", "T", "CARTA='" & CARTA & "'"), NrTot = DCount("IMPORTO", "T", "CARTA='" & CARTA & "'")
Based this sql is possible to summ the result to the current value in Importo and in nrtot with a where on db_carte field data1=12/11/2013?

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: summ and update

Post by HansV »

You can add a WHERE clause to the SQL.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: summ and update

Post by sal21 »

HansV wrote:You can add a WHERE clause to the SQL.

ok for tath.
but i need to summ the result of query to the current value in the fields NrTot and IMPORTO...

before to run query i have:
NrTot =2
IMPORTO=100

result of query
NrTot =1
IMPORTO=500

i need:
NrTot =3
IMPORTO=600

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: summ and update

Post by HansV »

OK, so you need to add the DSum result to the value of IMPORTO and of NrTot. So you use

... SET IMPORTO = IMPORTO + DSum(...), NrTot = NrTot + DSum(...) WHERE ...
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: summ and update

Post by sal21 »

HansV wrote:OK, so you need to add the DSum result to the value of IMPORTO and of NrTot. So you use

... SET IMPORTO = IMPORTO + DSum(...), NrTot = NrTot + DSum(...) WHERE ...

peraphs:

NrTot = NrTot + DCount

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: summ and update

Post by HansV »

Yes, OK, you're correct.
Best wishes,
Hans