从另一个查询访问SQL查询

问题描述 投票:0回答:1

我对一段SQL代码有点问题。我有一个表Paiements_17_18,我想创建一个计算的单行查询:

  • Amount字段的总和,
  • Date_Regulation字段的第一个日期,
  • Date_Regulation字段的最后日期,
  • N_Facture字段的不同值。

这一切都来自SELECT TOP n FROM ....风格的子请求

我试过这个:

SELECT Sum(P.Montant) AS TotalMontant, 
       First(P.Date_Regulation) AS PremièreDate, 
       Last(P.Date_Regulation) AS DernièreDate, 
       First(P.N_Facture) AS PremièreFacture, 
       Last(P.N_Facture) AS DernièreFacture, 
       (SELECT Count(N_Facture) 
        FROM (SELECT DISTINCT N_Facture FROM Paiements_17_18)) AS NombreFactures

FROM (SELECT TOP 5 Paiements_17_18.* 
      FROM Paiements_17_18 
      ORDER BY Paiements_17_18.ID_Paiement DESC) AS P;

但我收到“P”的错误

(Microsoft Access数据库引擎找不到输入表或查询“P”。确保它存在并且其名称拼写正确)

你能帮我吗?

sql ms-access subquery
1个回答
1
投票

生成NombreFacture字段的2行导致错误:

(SELECT Count(N_Facture) 
        FROM (SELECT DISTINCT N_Facture FROM Paiements_17_18)) AS 
NombreFactures

取代了两条线。见下文。

SELECT Sum(P.Montant) AS TotalMontant, 
       First(P.Date_Regulation) AS PremièreDate, 
       Last(P.Date_Regulation) AS DernièreDate, 
       First(P.N_Facture) AS PremièreFacture, 
       Last(P.N_Facture) AS DernièreFacture, 
       (SELECT Count(n.N_Facture_distinct) 
        FROM (SELECT DISTINCT N_Facture as N_facture_distinct FROM Paiements_17_18 ) AS n) 
        AS NombreFacture 
FROM (SELECT TOP 5 Paiements_17_18.* 
      FROM Paiements_17_18 
      ORDER BY Paiements_17_18.ID_Paiement DESC) AS P;
© www.soinside.com 2019 - 2024. All rights reserved.