访问查询:如何查询获取计算值?

时间:2008-12-10 09:39:25

标签: sql ms-access

HI,

我有一个大表,我可以从中查询以获取下表

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%

我如何编写查询来获取此信息?

由于

2 个答案:

答案 0 :(得分:2)

假设有这样一个表:

type, code, ... other columns.

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

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];

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

答案 1 :(得分:2)

怎么样:

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是大表的名称。