Results 1 to 4 of 4
  1. #1
    MrSmith is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jul 2013
    Posts
    3

    Access 2007 OVER clause workaround

    I am attempting to add an additional field to a table. The field will be a percentage of 100% in that TotalPrice/Sum([TotalPrice]). I don't believe the OVER clause works in Access, so was curious if someone had a suggestion in terms of a workaround. My SQL is below, it's receiving a "missing operator error" currently. I tried to create a new query that would return only SUM([TotalPrice]) and then use that query as my denominator but returned no results. Any suggestions would be heavily appreciated. Thanks!

    SELECT [2-C: Avg Prices by Customer Number Query].[Part Number],
    [2-C: Avg Prices by Customer Number Query].[Customer Name],
    [2-C: Avg Prices by Customer Number Query].[Customer Number],
    [2-C: Avg Prices by Customer Number Query].SumOfQuantity,
    [2-C: Avg Prices by Customer Number Query].TotalCost,
    [2-C: Avg Prices by Customer Number Query].TotalPrice,
    [2-C: Avg Prices by Customer Number Query].[Gross Margin],


    [TotalPrice] / SUM([TotalPrice]) OVER() AS WeightFactor
    FROM [2-C: Avg Prices by Customer Number Query]

  2. #2
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742

    Subquery for your Sum, luckily not correlated, I believe

    You'll need a subquery in that spot. I'm not familiar with Over() - since there are no parameters, I assume that's going to give you the total from the whole query. So, include the same query again with an alias, and alias the first query just to reduce the typing, and you get something like this -
    Code:
    SELECT 
    Q1.[Part Number],
    Q1.[Customer Name],
    Q1.[Customer Number],
    Q1.[SumOfQuantity],
    Q1.[TotalCost],
    Q1.[TotalPrice],
    Q1.[Gross Margin],
    Q1.[TotalPrice] / Q2.SumTotalPrice AS WeightFactor
    FROM [2-C: Avg Prices by Customer Number Query] AS Q1,
      (SELECT SUM(Q2.[TotalPrice]) As SumTotalPrice
      FROM [2-C: Avg Prices by Customer Number Query] AS Q2);
    If Over() was doing some secret handling of grouping, like only giving you the Sum for each [Customer Number], then you'd have to include all the grouping fields into the Q2 subquery and specify the JOIN criteria.

    That would look somewhat like this one, but it's total aircode....
    Code:
    SELECT 
    Q1.[Part Number],
    Q1.[Customer Name],
    Q1.[Customer Number],
    Q1.[SumOfQuantity],
    Q1.[TotalCost],
    Q1.[TotalPrice],
    Q1.[Gross Margin],
    Q1.[TotalPrice] / Q2.SumTotalPrice AS WeightFactor
    FROM [2-C: Avg Prices by Customer Number Query] AS Q1
    INNER JOIN
      (SELECT Q2.[Customer Number], SUM(Q2.[TotalPrice]) As SumTotalPrice
      FROM [2-C: Avg Prices by Customer Number Query] AS Q2
      GROUP BY Q2.[Customer Number])
    ON Q2.[Customer Number] = Q1.[Customer Number]
    );
    This thread had some correlated subquery SQL that was more solid: https://www.accessforums.net/access/...ess-35607.html

  3. #3
    MrSmith is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jul 2013
    Posts
    3
    Excellent thanks for your help!

  4. #4
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    You're welcome.

    It looks like ... " SUM([TotalPrice]) OVER([Customer Number]) AS SumTotalPrice " ... serves the same function in Oracle and SQL Server as that entire semi-correlated subquery in my second example does in Access/Jet SQL. (Okay, it's technically not a subquery at all, but I came *that* close to coding it as one, before I thought of a better way.)

    I'd love to see that clause implemented in Access. I don't know how they'd represent it Visually, but it would simplify the joins/SQL immensely for "partitions" that people need all the time.

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

Similar Threads

  1. Is there a workaround for the dbo_ bug?
    By GuideRWhite in forum Access
    Replies: 11
    Last Post: 10-23-2012, 03:06 PM
  2. Replies: 4
    Last Post: 05-18-2012, 06:07 PM
  3. Workaround for MINUS (with Where)
    By Autoclave in forum Queries
    Replies: 2
    Last Post: 10-31-2011, 08:15 AM
  4. Replies: 2
    Last Post: 06-18-2011, 09:55 AM
  5. Replies: 3
    Last Post: 01-05-2010, 10:07 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