Results 1 to 2 of 2
  1. #1
    Adsens is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2018
    Posts
    1

    SQL Query Filter and SUM

    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;

  2. #2
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Quote Originally Posted by Adsens View Post
    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;
    You probably solved this weeks ago but if not, change the double quotes to single quotes in each line of the WHERE statements e.g. 'Especes'
    Having said that, I'm not sure that you can use multiple subqueries like this ...but I've never tried!
    If it does work, I imagine it will be VERY slow to run
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Filter Query
    By romi in forum Queries
    Replies: 4
    Last Post: 12-27-2017, 02:00 AM
  2. Replies: 2
    Last Post: 02-20-2017, 11:28 PM
  3. Replies: 7
    Last Post: 05-27-2015, 12:45 PM
  4. Replies: 2
    Last Post: 01-30-2013, 07:34 PM
  5. Filter or Query? Help!
    By Niki in forum Access
    Replies: 2
    Last Post: 04-26-2011, 02:11 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums