8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

使用 SQL 在 MS-Access 查询中运行总计

Brett Donald 1月前

22 0

我想使用 SQL(请不要使用 DSUM)计算我的某个 Access 查询中的累计总数。我现有的查询中有 3 个字段,即 SubCategoryID、品牌和收入,我想计算...

我想使用 SQL(请不要使用 DSUM)计算我的某个访问查询中的累计总数。我现有的查询中有 3 个字段,即 SubCategoryID、品牌和收入,我想计算每个 SubCategory 下的收入累计总数。

我已经为其编写了一个查询,但是存在一些错误,我找不到纠正它的方法。

    SELECT   x.SubCategoryID
            ,x.brand
            ,x.[Avg Revenue]
              ( Select Sum(x2.[Avg Revenue]) 
                From FROM [BrandRevenue] x2
                Where x2.[SubCategoryID] = x.[SubCategoryID] AND x2.[Avg Revenue] <= x.[Avg Revenue]) As [Running Sum]
    FROM (Select SubCategoryID, brand, [Avg Revenue]
          FROM [BrandRevenue]
          Order BY SubCategoryID, [Avg Revenue] DESC) As x
    Group BY x.SubCategoryID, x.brand, x.[Avg Revenue];

谢谢你的帮助:)

帖子版权声明 1、本帖标题:使用 SQL 在 MS-Access 查询中运行总计
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由Brett Donald在本站《ms-access》版块原创发布, 转载请注明出处!
最新回复 (0)
  • 查询表达式中的语法错误“选择 Sum(x2.[Avg Revenue]) From FROM [BrandRevenue(Weekly)] x2 其中 x2.[SubCategoryID] = x.[SubCategoryID] AND x2.[Avg Revenue] <= x.[Avg Revenue])

  • 您在相关子查询中重复了“From”:

      ( Select Sum(x2.[Avg Revenue]) 
        From FROM [BrandRevenue] x2
        ...
    

    我认为您不需要 from 子句中的子查询,也不需要 GROUP BY。我认为这会更好,而且肯定更简单:

    SELECT  x.SubCategoryID,
            x.brand,
            x.[Avg Revenue],
            (   SELECT  SUM(x2.[Avg Revenue]) 
                FROM    [BrandRevenue] x2
                WHERE   x2.[SubCategoryID] = x.[SubCategoryID] 
                AND     x2.[Avg Revenue] <= x.[Avg Revenue]
            ) AS [Running Sum]
    FROM    BrandRevenue AS x
    ORDER BY x.SubCategoryID, x.[Avg Revenue] DESC;
    

    附录

    我认为要解决品牌具有相同收入的问题,您需要在相关子查询中添加一些额外的逻辑:

    SELECT  x.SubCategoryID,
            x.brand,
            x.[Avg Revenue],
            (   SELECT  SUM(x2.[Avg Revenue]) 
                FROM    [BrandRevenue] x2
                WHERE   x2.[SubCategoryID] = x.[SubCategoryID] 
                AND  (  x2.[Avg Revenue] <= x.[Avg Revenue]
                    OR  (x2.[Avg Revenue] = x.[Avg Revenue] 
                    AND x2.Brand <= x.Brand
                    )
            ) AS [Running Sum]
    FROM    BrandRevenue AS x
    ORDER BY x.SubCategoryID, x.[Avg Revenue] DESC, x.Brand;
    
  • 但有一个问题,当两个品牌的收入相同时,结果并不如预期。有办法解决这个问题吗?谢谢

返回
作者最近主题: