SQL - 交叉表查询中的条件语句 - 说什么

jer*_*rry 2 sql ms-access

我正在使用MS Access 2007.我有2个表:苏打的类型可喜性.

苏打的类型有:可乐,百事可乐,胡椒博士和梅洛黄

可喜性是这些选项的查找:喜欢,不喜欢,没有偏好

我知道如何使用DCount("[Types]","[Type of Soda]","[Types]"='Coke')计算表中的Cokes或Mello Yellows的数量

我也知道如何计算喜欢,不喜欢,没有偏好的人数.

("[Perception]","[Likeability]","[Perception]"=''喜欢')

但是,如果我需要按类型计算"喜欢"的数量,该怎么办?

即表格应如下所示:

             Coke     |        Pepsi       |       Dr. Pepper     |   Mello Yellow   

Likes          9                   2                    12                  19

Dislikes       2                   45                   1                    0

No Preference  0                   12                   14                  15 
Run Code Online (Sandbox Code Playgroud)

我知道在Access中我可以创建一个交叉表查询,但我的表是通过ID连接的.所以我的[Likeability]表有一个ID列,它与我的[Types]表中的ID列相同.这就是关系,这就是连接我的桌​​子的原因.

我的问题是我不知道如何应用条件来计算喜欢,不喜欢等,仅适用于我指定的类型.看起来我首先必须检查[Likeness]表中的"Likes",并在[Types]表中交叉引用ID和ID.

我很困惑,你现在也可能.但我想做的就是计算每种苏打水的喜欢和不喜欢的数量.

请帮忙.

Con*_*rix 5

它不是很清楚(对我来说)你的表是什么样的,所以我们假设如下

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)
Run Code Online (Sandbox Code Playgroud)

数据

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
Run Code Online (Sandbox Code Playgroud)

转换查询您可以编写这样的查询

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;
Run Code Online (Sandbox Code Playgroud)

查询输出

Perception_Name Coke Dr_ Pepper Mello Yellow Pepsi
--------------- ---- ---------- ------------ -----
Dislikes        1    1          1            3
Likes           2    1          1            1
Run Code Online (Sandbox Code Playgroud)