从Access访问多个字段

时间:2014-02-14 10:11:45

标签: sql ms-access

我为学校管理层建立了一个访问应用程序,我必须为学生列表提供报告。我存储我的数据的方式如下所示。

Student Std Gender  Cast
abc1    1   Male    Cast1
abc2    1   Female  Cast3
abc3    1   Male    Cast2
abc4    1   Male    Cast3
abc5    1   Male    Cast2
abc7    2   Male    Cast2
abc8    2   Female  Cast1
abc9    3   Male    Cast1
abc10   3   Male    Cast3
abc11   3   Female  Cast2
abc12   3   Male    Cast1

但是,需要以不同的格式查看数据。我需要的方式如下所示:

           Male         Female                  
Std Cast1Cast2Cast3  Cast1Cast2Cast3 TotalMale TotalFemale Total
1   1     2    1      0    0    1       4       1           5
2   0     1    0      1    0    0       1       1           2
3   2     0    1      0    1    0       3       1           4

我曾试图使用枢轴。但我无法在多列上转动表格。所以请帮助生成所需输出的查询。

1 个答案:

答案 0 :(得分:3)

您可以通过将字段值连接在一起来完成PIVOTing对多个字段的影响。例如

TRANSFORM Count(Student) AS CountOfStudents
SELECT Std
FROM Students
GROUP BY Std
PIVOT Gender & "_" & Cast

将返回

Std  Female_Cast1  Female_Cast2  Female_Cast3  Male_Cast1  Male_Cast2  Male_Cast3
---  ------------  ------------  ------------  ----------  ----------  ----------
  1                                         1           1           2           1
  2             1                                                   1            
  3                           1                         2                       1