我在微软访问中有1个表,其中包含学生信息(包括性别和成绩)。我试图使用SQL视图来计算每个年级中男性和女性的数量。现在,这就是我所拥有的。
SELECT
StudDetails.CurrClass as 'class',
Count(StudeDetails.sex) WHERE (StudDetails.sex="M" AND StudDetails.CurrClass='class') AS 'malecount', Count (StudeDetails.sex) WHERE (StudDetails.sex="F"AND StudDetails.CurrClass='class') AS 'femalecount'
FROM StudDetails
GROUP BY StudDetails.CurrClass;
我知道这非常糟糕,但我是SQL的新手。我尝试使用谷歌搜索,但我不理解答案或不能应用它们。
答案 0 :(得分:1)
给出表格中的两个字段 - Class 和 Gender
左侧的表格将使用此SQL显示右侧显示的结果:
SELECT Class
, Sex
, COUNT(Sex) AS CountOfGender
FROM StudDetails
GROUP BY Class, Sex
这里唯一的缺点是,如果班级中没有某种性别,它就不会显示0
- 它只是不会显示该性别。
+-------+--------+ +-------+--------+---------------+
| Class | Sex | | Class | Sex | CountOfSex |
+-------+--------+ +-------+--------+---------------+
| A | M | | A | F | 2 |
+-------+--------+ +-------+--------+---------------+
| B | M | | A | M | 3 |
+-------+--------+ +-------+--------+---------------+
| C | F | | B | M | 1 |
+-------+--------+ +-------+--------+---------------+
| A | F | | C | F | 1 |
+-------+--------+ +-------+--------+---------------+
| A | M | | D | M | 2 |
+-------+--------+ +-------+--------+---------------+
| A | M |
+-------+--------+
| D | M |
+-------+--------+
| D | M |
+-------+--------+
| A | F |
+-------+--------+
修改强>
让男性和女性女性在单独的字段中并包含0
,然后您可以只是交叉标记上面的查询:
TRANSFORM NZ(Sum(CountOfSex),0) AS CountOfGender
SELECT Class
FROM (
SELECT Class
, Sex
, COUNT(Sex) AS CountOfSex
FROM StudDetails
GROUP BY Class, Sex
)
GROUP BY Class
PIVOT Sex
+-------+---+---+
| Class | F | M |
+-------+---+---+
| A | 2 | 3 |
+-------+---+---+
| B | 0 | 1 |
+-------+---+---+
| C | 1 | 0 |
+-------+---+---+
| D | 0 | 2 |
+-------+---+---+
再次编辑:此方法的优点在于有条件地计算男性和&女性(IIF sex='M'.. etc
)是指如果有人决定他们不想确定为男性或女性,那么它将很乐意列出您能想到的任何其他性别。
+-------+---+---+---+
| Class | F | M | Z |
+-------+---+---+---+
| A | 2 | 3 | 0 |
+-------+---+---+---+
| B | 0 | 1 | 0 |
+-------+---+---+---+
| C | 1 | 0 | 0 |
+-------+---+---+---+
| D | 0 | 2 | 1 |
+-------+---+---+---+
答案 1 :(得分:0)
您无法以WHERE
的方式使用IIF
。您正在寻找Count
进行比较。
由于Sum
相当不灵活,因此使用SELECT
StudDetails.CurrClass as 'class',
Sum(IIF(StudDetails.sex="M", 1, 0)) AS malecount,
Sum(IIF(StudDetails.sex="M", 0, 1)) AS femalecount
FROM StudDetails
GROUP BY StudDetails.CurrClass;
并在比较中分配1和0更容易。
for pattern in "$@"; do
...
done
答案 2 :(得分:0)
我不确定ms访问功能,所以这些可能性并不适合你,但至少应该有一个。
-- Some sample data
create table StudDetails(sex char(1),currclass varchar(10));
insert into StudDetails values
('M','ClassA'), ('M','ClassA'), ('F','ClassA'),
('F','ClassB'), ('F','ClassB'), ('F','ClassB'), ('M','ClassB'), ('F','ClassB'),
('F','ClassC'), ('F','ClassC');
-- One method
select CurrClass as "class",
sum(case when sex='M' then 1 else 0 end) as "malecount",
sum(case when sex='F' then 1 else 0 end) as "femalecount"
from StudDetails
group by CurrClass;
-- Another method
with xxx(CurrClass,malecount,femalecount) as
(
select CurrClass,1,0 from StudDetails where sex = 'M'
union all
select CurrClass,0,1 from StudDetails where sex = 'F'
)
select CurrClass,sum(malecount) as malecount,sum(femalecount) as femalecount
from xxx
group by CurrClass;
-- Yet another method
select CurrClass,sum(malecount) as malecount,sum(femalecount) as femalecount
from (
select CurrClass,1 as malecount,0 as femalecount from StudDetails where sex = 'M'
union all
select CurrClass,0 as malecount,1 as femalecount from StudDetails where sex = 'F'
) xxx
group by CurrClass;