开发者

Sum from sum in mysql

开发者 https://www.devze.com 2023-02-18 04:42 出处:网络
SELECT f.nummer AS factuur_id, f.mvoor AS factuur, f.faktuurnr AS factuur_nr, f.topdatum AS factuur_datum,
 SELECT 
f.nummer AS factuur_id,
f.mvoor AS factuur,
f.faktuurnr AS factuur_nr,
f.topdatum AS factuur_datum,
f.vervaldag AS factuur_vervaldat开发者_如何学Cum,
f.totbruto AS factuur_totaal,
SUM(c.totbruto) AS credit_totaal,
SUM(b.bedrag) AS bedrag_totaal,
f.totbruto - (SUM(c.totbruto) + SUM(b.bedrag)) AS SOM

FROM    ((facturen AS f LEFT JOIN creditnota AS c ON f.nummer = c.nummer)

LEFT JOIN betaling1 AS b ON f.nummer = b.factuurnr)

LEFT JOIN klanten AS k ON f.klantnr = k.nr

WHERE f.betaald = 'N'

AND CURDATE() >= DATE_ADD(f.vervaldag, INTERVAL 15 DAY)

GROUP BY f.nummer

HAVING  (factuur_totaal - (SUM(c.totbruto) + SUM(b.bedrag))) > 0 

ORDER BY k.naam

And now I want a SUM from the SOM Do I need a subquery for this? And how to I do this?

I know I can just loop it but I want it in 1 query.

mysql, php

Thanks!


You can do this:

SELECT SUM(SOM) FROM (
    SELECT 
    f.nummer AS factuur_id,
    f.mvoor AS factuur,
    f.faktuurnr AS factuur_nr,
    f.topdatum AS factuur_datum,
    f.vervaldag AS factuur_vervaldatum,
    f.totbruto AS factuur_totaal,
    SUM(c.totbruto) AS credit_totaal,
    SUM(b.bedrag) AS bedrag_totaal,
    f.totbruto - (SUM(c.totbruto) + SUM(b.bedrag)) AS SOM

FROM    ((facturen AS f LEFT JOIN creditnota AS c ON f.nummer = c.nummer)
LEFT JOIN betaling1 AS b ON f.nummer = b.factuurnr)
LEFT JOIN klanten AS k ON f.klantnr = k.nr
WHERE f.betaald = 'N'
AND CURDATE() >= DATE_ADD(f.vervaldag, INTERVAL 15 DAY)
GROUP BY f.nummer
HAVING  (factuur_totaal - (SUM(c.totbruto) + SUM(b.bedrag))) > 0 
ORDER BY k.naam) AS temp

Basically I've just wrapped your query in a SELECT SUM(<some row>) FROM (<your query>) AS temp.


Wrap the SOM statement in another SUM() call:

SELECT ...
    SUM(f.totbruto - (SUM(c.totbruto) + SUM(b.bedrag))) AS SOM
...
0

精彩评论

暂无评论...
验证码 换一张
取 消