从另一个查询

时间:2018-01-04 15:05:02

标签: sql ms-access subquery

我对一段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”。确保它存在并且其名称拼写   正确地)

你能帮我吗?

1 个答案:

答案 0 :(得分:1)

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

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;

替换了两行。见下文。

DateTime.Now