在Access VBA中创建频率表

时间:2012-08-29 20:57:23

标签: ms-access access-vba

我有一张桌子,让不同的参与者在多天内获得多盒药品。我正在尝试创建一个频率表,显示已经按照参与者的箱数分配了多少药品 enter image description here

我正在寻找的结果是 -

2盒= 1(因为只有Lynda共有2盒),4盒= 2(因为Ryan和Rinky在加完药盒后共得到4盒)

请让我知道在这种情况下哪种方法最好。 谢谢你的帮助。
-Nams

3 个答案:

答案 0 :(得分:1)

我想你想要:

SELECT t.SumOf, Count(t.[PARTICIPANT ID]) AS CountOf
FROM (SELECT Table1.[PARTICIPANT ID], Sum(Table1.MEDICINE_BOX) AS SumOf
FROM Table1
GROUP BY Table1.[PARTICIPANT ID])  AS t
GROUP BY t.SumOf;

其中table1是表的名称。

答案 1 :(得分:0)

如果您的表格如下:

medicine_dispense
participantID   date    amount_boxes
ABC             8/29/12  1
ABC             8/30/12  2
XYZ             8/29/12  1
XYZ             8/30/12  1

然后是这样的查询:

select
amount_boxes, count(participantID)
from
medicine_dispense

应该有效

答案 2 :(得分:0)

我将使用通用SQL。您可以在SQL视图中将SQL粘贴到Access查询中。 (您可能必须删除CHECK()约束。)

create table participant_meds (
  participant varchar(10) not null,
  distribution_date date not null default current_date,
  num_boxes integer not null check (num_boxes > 0),
  primary key (participant, distribution_date)
);

insert into participant_meds values ('Ryan', '2012-02-03', 1);
insert into participant_meds values ('Ryan', '2012-06-07', 3);
insert into participant_meds values ('Rinky', '2012-02-28', 4);
insert into participant_meds values ('Lynda', '2012-03-04', 2);
insert into participant_meds values ('Russ', '2012-04-05', 2);
insert into participant_meds values ('Russ', '2012-05-08', 2);
insert into participant_meds values ('Russ', '2012-06-12', 2);

生成的数据,已分类,可用于复制/粘贴。

participant distribution_date   num_boxes
Lynda   2012-03-04  2
Rinky   2012-02-28  4
Russ     2012-04-05 2
Russ     2012-05-08 2
Russ     2012-06-12 2
Ryan     2012-02-03 1
Ryan     2012-06-07 3

此查询为您提供每位参与者的总框数。

select sum(num_boxes) boxes, participant
from participant_meds
group by participant;

6;"Russ"
2;"Lynda"
4;"Ryan"
4;"Rinky"

在FROM子句中使用该查询,就好像它是一个表一样。 (我考虑将该查询存储为视图,因为我怀疑每个参与者的盒子总数可能很有用。此外,Access一直擅长优化使用视图的查询。)

select boxes num_boxes, count(participant) num_participants
from (select sum(num_boxes) boxes, participant
      from participant_meds
      group by participant) total_boxes
group by num_boxes
order by num_boxes;

num_boxes  num_participants
--
2          1
4          2
6          1