Results 1 to 3 of 3

Thread: Why this MS Access query counts row values twice

  1. #1

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    206

    Why this MS Access query counts row values twice

    I have this query being generated at runtime:

    Code:
    SELECT 
    
    1 as red, 
    " VKUPNO" AS UnvierzitetFakultet, 
    Sum(IIf([TAB]=11 And [RED]=1,[K1],0)) AS Se, 
    Sum(IIf([TAB]=11 And [RED]=1,[K2],0)) AS zeni, 
    Sum(IIf([TAB]=11 And [RED]=1,[K1],0))-Sum(IIf([TAB]=11 And [RED]=1,[K2],0)) AS Mazi, 
    Sum(IIf([TAB]=5 And [RED]=2 Or [TAB]=6 And [RED]=2,[K1],0)) AS [Doktor po Nauki], 
    Sum(IIf([TAB]=5 And [RED]=3 Or [TAB]=6 And [RED]=3,[K1],0)) AS [Magistri po Nauki], 
    Sum(IIf([TAB]=5 And [RED]=4 Or [TAB]=6 And [RED]=4,[K1],0)) AS Specijalisti, 
    Sum(IIf([TAB]=5 And [RED]=5 Or [TAB]=6 And [RED]=5,[K1],0)) AS [So visoko obrazovanie], 
    Sum(IIf([TAB]=5 And [RED]=1 Or [TAB]=6 And [RED]=1,[K3],0)) AS [so polno rabotno vreme], 
    Sum(IIf([TAB]=5 And [RED]=1 Or [TAB]=6 And [RED]=1,[K5],0)) AS [so pokratko rabotno vreme] 
    
    INTO IT3
    FROM Fakulteti
    INNER JOIN Vnes 
    ON [Fakulteti].[RBR]=[Vnes].[RBR] 
    GROUP BY 1, " VKUPNO";
    For some reason, the 1 that I add as a row number makes the rest of the query sum/count the values twice, i.e twice bigger numbers are added to table IT3.

    Any ideas what happens in this case?
    Why that 1 at the top confuses the Access SQL engine?

    Tried replacing 1 with 0. Then the values of the new row were all zeroes for some reason. As if that column has some influence on the calculation.

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,071

    Re: Why this MS Access query counts row values twice

    At a guess:

    Code:
    Sum(IIf([TAB]=5 And [RED]=2 Or [TAB]=6 And [RED]=2,[K1],0)) AS [Doktor po Nauki],
    (and also the following lines)

    Let's break it down
    Code:
    Sum(
      IIf(
        [TAB]=5 And [RED]=2 Or [TAB]=6 And [RED]=2,[K1],0
      )
        ) AS [Doktor po Nauki],

    Is Access respecting Precedence of logic operators, when NOT using Paranthesis'?

    btw: You should "alias" your table-names so people can see which field belongs to which table.

    EDIT:
    For above i would even go with the "shortcut":

    IIf(
    ([TAB]=5 Or [TAB]=6) And [RED]=2,[K1],0
    )

    EDIT2: Your GROUP BY looks weird
    GROUP BY 1, " VKUPNO";

    The first criterium: Is it Grouping by the Value or by the Ordinal?

    EDIT3: As a Test: rename your very first field (--> 1 As red) into something else.
    I don't remember anymore, but i think Access allows using Aliases of preceding fields in Calculations,
    so your IIF's might take the 1 from the first Field for [RED] instead of the Table-Column

    Tried replacing 1 with 0. Then the values of the new row were all zeroes for some reason. As if that column has some influence on the calculation.
    Actually, i'm pretty sure it does take the Value from your first Field, since you declare "red" to be 0, and your IIF's all resolve to 0 if the conditions are not met, and inside the IIF you don't compare [RED] to 0 anywhere, so it would explain why you end up with zeros

    All said: Alias your Tables, and qualify each Column with its Table-Alias

    Code:
    SELECT 
    
    1 as red, 
    " VKUPNO" AS UnvierzitetFakultet, 
    Sum(IIf([T1.TAB]=11 And [T2.RED]=1,[T2.K1],0)) AS Se, -- or to whichever Table each column belongs
    
    *snipp*
    
    INTO IT3
    FROM Fakulteti AS T1
    INNER JOIN Vnes AS T2
    ON T1.[RBR]=T2.[RBR] 
    GROUP BY 1, " VKUPNO";
    Last edited by Zvoni; May 19th, 2025 at 05:51 AM.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  3. #3

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    206

    Re: Why this MS Access query counts row values twice

    1 as red,
    " VKUPNO" AS UnvierzitetFakultet,

    are just labels for the row that will appear in the final table

    Yea adding alias for the table which should be just [Vnes] everywhere and appropriate parenthesis did the job.
    Last edited by kutlesh; May 19th, 2025 at 09:29 AM.

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
  •  



Click Here to Expand Forum to Full Width