简体   繁体   中英

How to change column field from the table by using joins in MS ACCESS?

Ok, I had very long research about this problem, and can't get the answer, so I hope you can help :)

The problem:

When I use this sql query in Access:

SELECT PARTNERI.SIFRA, Sum(FAKTURE.ZA_UPLATU)-Sum(UPLATEF.IZNOS) + SUM(PARTNERI.DUGUJE)
FROM ((UPLATEF RIGHT JOIN PARTNERI ON UPLATEF.SIFRA = PARTNERI.SIFRA) LEFT JOIN FAKTURE ON PARTNERI.SIFRA = FAKTURE.S_KUP)
GROUP BY PARTNERI.SIFRA;

I get a resultset and it's fine, but... I want to update column from PARTNERI table, and I wrote this:

UPDATE PARTNERI SET NG_DUGUJE_DOB = 
SELECT Sum(FAKTURE.ZA_UPLATU)-Sum(UPLATEF.IZNOS) + SUM(PARTNERI.DUGUJE)
FROM ((UPLATEF RIGHT JOIN PARTNERI ON UPLATEF.SIFRA = PARTNERI.SIFRA) LEFT JOIN FAKTURE ON PARTNERI.SIFRA = FAKTURE.S_KUP)

;

And I get a syntax error...I don't know what to do... If you know the answer please let me know... PS I'm beginner of using SQL... Thanks :)

Edit:

Ok, I tried something and wrote this:

   UPDATE PARTNERI SET PARTNERI.NG_DUGUJE_DOB = 
(SELECT Sum(FAKTURE.ZA_UPLATU)-Sum(UPLATEF.IZNOS) + SUM(PARTNERI.DUGUJE)
FROM ((UPLATEF RIGHT JOIN PARTNERI ON UPLATEF.SIFRA = PARTNERI.SIFRA) LEFT JOIN FAKTURE ON PARTNERI.SIFRA = FAKTURE.S_KUP))

...this time I got this error:

Operation must use updateable query.

EDIT:

I have managed to solve the problem using DSum function that you guys told me and it works great!

Thank you very much for your answers, I'm very grateful for your help! :)

This is a known limitation of the Access Database Engine: daft as it may sound, it cannot UPDATE using a query involving an summarization (aggregate), even when using its own proprietary syntax, and there is no 'pure SQL' workaround:

Microsoft Help and Support: Update Query Based on Totals Query Fails

I'm on record as saying that Access's UPDATE syntax is its worst failure.

尝试这样做:

UPDATE  (PARTNERI LEFT JOIN UPLATEF ON UPLATEF.SIFRA = PARTNERI.SIFRA) LEFT JOIN FAKTURE ON PARTNERI.SIFRA = FAKTURE.S_KUP SET PARTNERI.NG_DUGUJE_DOB = Sum(FAKTURE.ZA_UPLATU)-Sum(UPLATEF.IZNOS) + SUM(PARTNERI.DUGUJE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM