Balance line by line
Solved
Oholabi12345
Posted messages
511
Status
Member
-
oholabi -
oholabi -
Bonjour, voici la requête modifiée pour calculer le solde ligne par ligne en tenant compte du solde précédent :
```sql
WITH Transactions AS (
SELECT
Commandes.idClient,
client.nomcomplet,
client.adresse,
client.tel,
client.ville,
Commandes.DateCommande,
Commandes.CodeFacture,
'Commande' AS operation,
Commandes.MontantTTC AS MONTANTDEBIT,
Commandes.MontantRegle AS MONTANTCREDIT
FROM client
RIGHT JOIN Commandes ON client.idclient = Commandes.idClient
UNION ALL
SELECT
Avance.Idlient,
client.nomcomplet,
client.adresse,
client.tel,
client.ville,
Avance.DateAvance,
'' AS CodeFacture,
'Avance' AS operation,
0 AS MONTANTDEBIT,
Avance.MontantAvance AS MONTANTCREDIT
FROM client
INNER JOIN Avance ON client.idclient = Avance.Idlient
)
SELECT
idClient,
nomcomplet,
adresse,
tel,
ville,
DateCommande,
CodeFacture,
operation,
MONTANTDEBIT,
MONTANTCREDIT,
SUM(MONTANTDEBIT - MONTANTCREDIT) OVER (PARTITION BY idClient ORDER BY DateCommande) AS Solde
FROM
Transactions
ORDER BY
DateCommande;
```
Cette requête utilise une CTE (Common Table Expression) pour rassembler les transactions, puis elle calcule le solde en utilisant la fonction de fenêtre `SUM() OVER ()`.
```sql
WITH Transactions AS (
SELECT
Commandes.idClient,
client.nomcomplet,
client.adresse,
client.tel,
client.ville,
Commandes.DateCommande,
Commandes.CodeFacture,
'Commande' AS operation,
Commandes.MontantTTC AS MONTANTDEBIT,
Commandes.MontantRegle AS MONTANTCREDIT
FROM client
RIGHT JOIN Commandes ON client.idclient = Commandes.idClient
UNION ALL
SELECT
Avance.Idlient,
client.nomcomplet,
client.adresse,
client.tel,
client.ville,
Avance.DateAvance,
'' AS CodeFacture,
'Avance' AS operation,
0 AS MONTANTDEBIT,
Avance.MontantAvance AS MONTANTCREDIT
FROM client
INNER JOIN Avance ON client.idclient = Avance.Idlient
)
SELECT
idClient,
nomcomplet,
adresse,
tel,
ville,
DateCommande,
CodeFacture,
operation,
MONTANTDEBIT,
MONTANTCREDIT,
SUM(MONTANTDEBIT - MONTANTCREDIT) OVER (PARTITION BY idClient ORDER BY DateCommande) AS Solde
FROM
Transactions
ORDER BY
DateCommande;
```
Cette requête utilise une CTE (Common Table Expression) pour rassembler les transactions, puis elle calcule le solde en utilisant la fonction de fenêtre `SUM() OVER ()`.
2 answers
-
yg_be Posted messages 23437 Registration date Status Contributor Last intervention Ambassadeur 1 588
Hello,
let's give a name to this request, let's call it det.
let's try this:select d1.idClient, d1.DateCommande, d1.nomcomplet, d1.adresse, d1.tel, d1.ville, d1.CodeFacture, d1.operation, d1.MONTANTDEBIT, d1.MONTANTCREDIT, sum(d2.MONTANTDEBIT - d2.MONTANTCREDIT) as solde from det as d1, det as d2 where d1.idClient = d2.idClient and d1.DateCommande >= d2.DateCommande group by d1.idClient, d1.DateCommande, d1.nomcomplet, d1.adresse, d1.tel, d1.ville, d1.CodeFacture, d1.operation, d1.MONTANTDEBIT, d1.MONTANTCREDIT order by d1.idClient, d1.DateCommande
-
yg_be Posted messages 23437 Registration date Status Contributor Last intervention Ambassadeur 1 588
Did you properly name thedet
query you shared at the beginning of the discussion?
Do you think the query takes into account a specific period?-
-
-
Let's take back your table.
If the period starts on 8/1, what balance do you want to have in the first line for 8/1: 60000 or 210000?
Should the balance of operations before the period be taken into account? -
-
-