I need to make a SQL query to do that:
1. SELECT TOP 6 Records from Table [Paiements_17_18].
2. Filter ONLY The 6 records by [Type_Reglements] = "Espèces".
3. Do a SUM of [Montant] field from the Filtred Records.
I Tried This but It's not working *(Where Condition)* :
Code:
SELECT S1.TotalMontantESP, S2.TotalMontantCHQ, S3.TotalMontantLCN, S4.NombreFactures, S5.PremièreDate, S5.DernièreDate, S5.PremièreFacture, S5.DernièreFacture, S5.NumeroCaisse
FROM
(SELECT sum(Montant) As TotalMontantESP FROM (SELECT TOP 6 Montant FROM Paiements_17_18) WHERE Type_Reglement = "Espèces") As P) As S1,
(SELECT sum(Montant) As TotalMontantCHQ FROM (SELECT TOP 6 Montant FROM Paiements_17_18) WHERE Type_Reglement = "Chèque") As P) As S2,
(SELECT sum(Montant) As TotalMontantLCN FROM (SELECT TOP 6 Montant FROM Paiements_17_18) WHERE Type_Reglement = "LCN") As P) As S3,
(SELECT Count(N_Facture) AS NombreFactures FROM (SELECT DISTINCT N_Facture FROM (SELECT TOP 6 N_Facture FROM Paiements_17_18) AS P1) AS P2) AS S4,
(SELECT Min([Date_Reglement]) AS PremièreDate, Max([Date_Reglement]) AS DernièreDate, Min([N_Facture]) AS PremièreFacture, Max([N_Facture]) AS DernièreFacture, Max([N_Caisse]) AS NumeroCaisse
FROM (SELECT TOP 6 Paiements_17_18.* FROM Paiements_17_18) AS P3) AS S5;