Balance line by line

Solved
Oholabi12345 Posted messages 511 Status Member -  
 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 ()`.

2 answers

  1. 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
    0
    1. Oholabi12345 Posted messages 511 Status Member 1
       
      Good evening, I am receiving the following message: Circular reference caused by << det>>
      Does the query take into account a specific period, meaning between two dates (datedebut) and (datefin)? Also, what do d1 and d2 used in the det query represent?
      0
  2. yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   Ambassadeur 1 588
     
    Did you properly name the
    det
    query you shared at the beginning of the discussion?
    Do you think the query takes into account a specific period?
    0
    1. yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588
       
      If you are working with a period, do you want the balance to be zero at the beginning of the period?
      0
    2. Oholabi12345 Posted messages 511 Status Member 1 > yg_be Posted messages 23437 Registration date   Status Contributor Last intervention  
       
      Hello, I don't see the importance of setting the stock to zero or not at the beginning of the period
      Could you explain the scope to me?
      0
    3. yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588 > Oholabi12345 Posted messages 511 Status Member
       
      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?
      0
    4. Oholabi12345 Posted messages 511 Status Member 1 > yg_be Posted messages 23437 Registration date   Status Contributor Last intervention  
       
      Yes, the previous balance must be taken into account unless it is zero.
      Thank you again.
      0
    5. yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588 > Oholabi12345 Posted messages 511 Status Member
       
      So he doesn't apply the period in the three basic queries to calculate the balances; he applies it afterwards.
      0