SQL查询过滤器和SUM

时间:2018-01-05 18:10:16

标签: sql ms-access

我需要创建一个SQL查询来执行此操作:

  1. 从表[Paiements_17_18]中选择前6个记录。
  2. 过滤 ONLY [Type_Reglements] =“Espèces”的6条记录。
  3. 执行[Montant]字段的SUM。
  4. 我试过这个,但它不起作用(Where Condition)

        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;
    

1 个答案:

答案 0 :(得分:0)

我建议:

SELECT sum(Montant)
FROM (SELECT TOP 6 p.*
      FROM Paiements_17_18 as p
      ORDER BY ID_Paiement DESC
     ) as p
WHERE T.Type_Reglement = "Espèces";

有点难以理解您的查询逻辑,但我非常确定您在获取where之后正在执行sum() 。< / p>