0

I have a little problem with a piece of SQL code. I have a table Paiements_17_18 and I would like to create a single-line query that calculates:

  • the total of the Amount field,
  • the first date of Date_Regulation field,
  • the last date of Date_Regulation field,
  • the distinct values of N_Facture field.

All this from a sub request of the style SELECT TOP n FROM ....

I tried this:

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;

But I get an error of "P"

(The Microsoft Access database engine cannot find the input table or query" P" . Make sure it exists and that its name is spelled correctly)

Can you help me please?

5
  • If you remove the 2 lines for generating NombreFactures, do you get error? Commented Jan 4, 2018 at 15:21
  • 2
    From my sample data following your logic, I cannot reproduce error. By looking at syntax I cannot see why the error is raised. Make sure this exact query is generating that error. Possibly your actual did not include table alias, P. Commented Jan 4, 2018 at 15:22
  • @kc2018; Yes when i remove the line generating NombreFactures, there is no more error !! Commented Jan 4, 2018 at 15:38
  • @Parfait; what do you mean by I didn't Include the table's alias P Commented Jan 4, 2018 at 15:42
  • I am saying in your actual query, you may hve missed the P at end even though here it is posted. Very odd! I tested that SELECT Count(...) FROM (SELECT DISTINCT ...) without an issue! Commented Jan 4, 2018 at 15:44

1 Answer 1

1

The 2 lines on generating the NombreFacture field is causing the error:

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

Replaced the two lines. See below.

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;
Sign up to request clarification or add additional context in comments.

8 Comments

@kc2018; I got an error "access circular reference caused by alias [N_Facture] in query definition's SELECT list" :(
I have changed the variable name from N_facture to N_facture_distinct
@HoucineAdsens ... please provide sample data of Paiments_17_18, so we can fully reproduce issue.
@kc2018; Finally it works, thank you thank you very very much, but I have another question if you allow me. I want to calculate The Destinct [N_Facture] from P. not from the Table! for exemple if i Have SELECT TOP 5, i want just count destinct [N_Facture] of the 5 first records. Not from all the table
@HoucineAdsens; Try count(distinct P.N_Facture) instead of the 2 lines of subquery.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.