【问题标题】:access query: how to query to get calculated values?访问查询:如何查询以获取计算值?
【发布时间】:2008-12-10 09:39:25
【问题描述】:

嗨,

我有一个大表,我可以从中查询得到下表

type       no of times type occurs
101            450
102            562
103            245

我也可以换一张桌子

code      no of times code occurs
0               1222
1                750 
2                355

但现在我想写一个查询,可以得到下表

type  no of timescode1occurs %of timescode1 occurs out of  %of times code1 occurs out of  
                              no of times type occurs       no of times code occcurs

101          50                11%                                  6%
102          75                13%                                  10%

我如何编写查询来得到这个?

谢谢

【问题讨论】:

    标签: sql ms-access


    【解决方案1】:

    怎么样:

    SELECT t.Type, t.Code, COUNT(t.Code) AS CountOfCode, 
      [CountOfCode]/DCount("Code","t","Code=" & [Code])*100 AS PercentCode, 
      [CountOfCode]/DCount("Type","t","Type=" & [Type])*100 AS PercentType
          FROM t
          GROUP BY t.Type, t.Code
    

    其中 t 是大表的名称。

    【讨论】:

    • 感谢您完成了这项工作。我也可以在查询本身中将百分比四舍五入到小数点
    • 我可以将其四舍五入,但我如何将 % 符号放入结果中
    • 一种方式:... Format([CountOfCode]/DCount("Type","t","Type=" & [Type]),"Percent") AS [% Type]
    • 如果我想绘制生成的表格的图表,我最好将它们导出到 excel 然后尝试绘制它们或在访问本身中进行绘制。无论哪种方式,我都想用 vba 来做。
    • 很难说。我会使用 Access,因为我熟悉它并且很少需要任何非常复杂的东西。您的情况可能不同。
    【解决方案2】:

    假设这样的表:

    type, code, ... other columns.
    

    我假设您的前 2 个查询类似于

    select type, count(*) from mytable group by type
    
    select code, count(*) from mytable group by code
    

    然后你想做类似的事情

    SELECT DISTINCTROW mytable.Type, mytable.Code, 
    Count(*)/q1.[Count of type] AS [Percent Of Type],
    Count(*)/q2.[Count of code] AS [Percent Of Code]
    FROM mytable, 
      (select type, count(*) as [Count of type] from mytable group by type) q1,
      (select code, count(*) as [Count of code] from mytable group by code) q2
    where mytable.Type =q1.Type
    and mytable.Code=q2.Code
    GROUP BY mytable.Type, mytable.Code, q1.[Count of type], q2.[Count of code];
    

    希望这会有所帮助。 克里斯

    【讨论】:

      猜你喜欢
      • 2021-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-23
      • 2010-11-27
      • 2015-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多