我正在与MS Access 2007合作。我有两个表:类型的Soda和可爱的。
苏打水的种类有:可乐、百事可乐、胡椒博士和麦洛黄。
喜欢性是一种有以下选项的查找:喜欢,不喜欢,没有偏好
我知道如何使用DCount(“类型”、“类型的苏打”、“类型”=“可口可乐”)来计算表中的可乐或美罗黄的数量。
我也知道如何数多少喜欢,不喜欢,没有偏好。
(“感知”、“可爱”、“感知”=“喜欢”)
但是,如果我需要按类型来计算“喜欢”的数量呢?
也就是说,该表应如下所示:
Coke | Pepsi | Dr. Pepper | Mello Yellow
Likes 9 2 12 19
Dislikes 2 45 1 0
No Preference 0 12 14 15 我知道在Access中我可以创建一个交叉选项卡查询,但是我的表是由一个ID连接起来的,所以我喜欢的表中有一个ID列,它与我的Types表中的ID列相同。这就是关系,这就是连接我的桌子的原因。
我的问题是,我不知道如何应用条件来计算喜欢、不喜欢等,只适用于我指定的类型。似乎我首先必须检查可爱表中的“喜欢”,并将ID与Types表中的ID交叉引用。
我很困惑,现在你可能也很困惑。但我所要做的就是数一数每一种苏打水的好恶。
请帮帮忙。
发布于 2011-06-16 16:51:50
不太清楚(对我来说)您的表是什么样子的,所以让我们假设如下
表
Soda
------
Soda_ID (Long Integer (Increment))
Soda_Name (Text(50)
Perception
------
Perception_ID (Long Integer (Increment))
Perception_Name (Text(50)
Likeability
-----------
Likeability_ID (Long Integer (Increment))
Soda_ID (Long Integer)
Perception_ID (Long Integer)
User_ID (Long Integer)数据
Soda_Id Soda_Name
------- ---------
1 Coke
2 Pepsi
3 Dr. Pepper
4 Mello Yellow
Perception_ID Perception_Name
------------- ---------
1 Likes
2 Dislikes
3 No Preference
Likeability_ID Soda_ID Perception_ID User_ID
-------------- ------- ------------- -------
1 1 1 1
2 2 1 1
3 3 1 1
4 4 1 1
5 1 2 2
6 2 2 2
7 3 2 2
8 4 2 2
9 1 3 3
10 2 3 3
11 3 3 3
12 4 3 3
13 1 1 5
14 2 2 6
15 2 2 7
16 3 3 8
17 3 3 9
18 3 3 10转换查询您可以编写这样的查询
TRANSFORM
Count(l.Likeability_ID) AS CountOfLikeability_ID
SELECT
p.Perception_Name
FROM
Soda s
INNER JOIN (Perception p
INNER JOIN Likeability l
ON p.Perception_ID = l.Perception_ID)
ON s.Soda_Id = l.Soda_ID
WHERE
p.Perception_Name<>"No Preference"
GROUP BY
p.Perception_Name
PIVOT
s.Soda_Name;查询输出
Perception_Name Coke Dr_ Pepper Mello Yellow Pepsi
--------------- ---- ---------- ------------ -----
Dislikes 1 1 1 3
Likes 2 1 1 1https://stackoverflow.com/questions/6375101
复制相似问题