• SQL: SUM with SELEC DISTINCT (Access 2003 / XP)

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » SQL: SUM with SELEC DISTINCT (Access 2003 / XP)

    Author
    Topic
    #408685

    Hi everybody:

    I’m using Access 2003 / XP and am getting inaccurate results using SELECT DISTINCT in a totals query. My select distinct statement produces correct results. However, when I view the totals row and sum up the fields, the duplicate rows are included. I don’t recall that this was a problem in previous versions of Access, so am hoping someone can throw some light on this.

    SELECT DISTINCT Sum(BalData.BegBalNet) AS SumOfBegBalNet, Sum(BalData.EndBalNet) AS SumOfEndBalNet, Investments.InvestmentId
    FROM Investments INNER JOIN (BalData INNER JOIN (People INNER JOIN InvestContactReportType ON People.PersonId = InvestContactReportType.PersonId) ON BalData.InvestmentId = InvestContactReportType.InvestmentId) ON Investments.InvestmentId = InvestContactReportType.InvestmentId
    WHERE (((People.PersonId)=2128640559) AND ((Investments.Investment)=’High Peak International Hldg.’) AND ((BalData.AsOfDate)=#1/31/2004#))
    GROUP BY People.PersonId, Investments.Investment, Investments.InvestmentId, BalData.AsOfDate;

    Apparently, Access SQL is performing the sums before filtering the records with the WHERE clause. Using a HAVING clause produced the same problem.

    I tried using IN statements in the criteria, but Access SQL produced no results. Can someone improve my SQL?

    SELECT Sum(BalData.BegBalNet) AS SumOfBegBalNet, Sum(BalData.EndBalNet) AS SumOfEndBalNet
    FROM BalData
    HAVING (((BalData.BegBalNet)) In (SELECT DISTINCT BalData.BegBalNet
    FROM Investments INNER JOIN (BalData INNER JOIN (People INNER JOIN InvestContactReportType ON People.PersonId = InvestContactReportType.PersonId) ON BalData.InvestmentId = InvestContactReportType.InvestmentId) ON Investments.InvestmentId = InvestContactReportType.InvestmentId
    WHERE (((People.PersonId)=2128640559) AND ((Investments.Investment)=’High Peak International Hldg.’) AND ((BalData.AsOfDate)=#1/31/2004#))) AND ((BalData.EndBalNet)) In (SELECT DISTINCT BalData.EndBalNet
    FROM Investments INNER JOIN (BalData INNER JOIN (People INNER JOIN InvestContactReportType ON People.PersonId = InvestContactReportType.PersonId) ON BalData.InvestmentId = InvestContactReportType.InvestmentId) ON Investments.InvestmentId = InvestContactReportType.InvestmentId
    WHERE (((People.PersonId)=2128640559) AND ((Investments.Investment)=’High Peak International Hldg.’) AND ((BalData.AsOfDate)=#1/31/2004#));

    I realize I can work around this by saving the SELECT DISTINCT as a querydef and then summing its results in a second query, but this approach means creating a new querydef each time my code runs. Since I’m passing in PersonID, AsOfDate and Investment variables, it makes it a bit messy . . .

    Any ideas?

    Viewing 1 reply thread
    Author
    Replies
    • #865022

      I think you’re stuck with creating two queries, a query to perform the SELECT DISTINCT and a totals query based on the first one to calculate the sums. I would recommend to get the values for the criteria from a form; that way, the user can enter or select the criteria and you will only need the two queries, instead of a new set for each value.
      Say that you create a form frmCriteria with a text box or combo box PersonID, a text box or combo box Investment and a text box or combo box AsOfDate. You can use this as WHERE clause in the first query:

      WHERE People.PersonId=Forms!frmCriteria!PersonID AND Investments.Investment=Forms!frmCriteria!Investment AND BalData.AsOfDate=Forms!frmCriteria!AsOfDate

      • #865074

        Thanks, Hans, that’s what I’ve done. After scrutinizing the KB, I discovered that Jet SQL does not support SUM(Distinct). Actually, it runs pretty fast.

      • #865075

        Thanks, Hans, that’s what I’ve done. After scrutinizing the KB, I discovered that Jet SQL does not support SUM(Distinct). Actually, it runs pretty fast.

    • #865023

      I think you’re stuck with creating two queries, a query to perform the SELECT DISTINCT and a totals query based on the first one to calculate the sums. I would recommend to get the values for the criteria from a form; that way, the user can enter or select the criteria and you will only need the two queries, instead of a new set for each value.
      Say that you create a form frmCriteria with a text box or combo box PersonID, a text box or combo box Investment and a text box or combo box AsOfDate. You can use this as WHERE clause in the first query:

      WHERE People.PersonId=Forms!frmCriteria!PersonID AND Investments.Investment=Forms!frmCriteria!Investment AND BalData.AsOfDate=Forms!frmCriteria!AsOfDate

    Viewing 1 reply thread
    Reply To: SQL: SUM with SELEC DISTINCT (Access 2003 / XP)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: