i have a table named attendance with 2 attributes (id, remarks). i want to display the tally of absence or late per id from the attendance table.
我有一个名为出勤的表,有2个属性(id,备注)。我想从出勤表中显示缺席或迟到的每个身份证。
Attendance Table
|ID | Remarks |
=============================
|1 | Absent |
|1 | Late |
|2 | Absent |
|2 | Absent |
|3 | Late |
Sample Output
|ID | Absent | Late |
==================================
|1 | 1 | 1 |
|2 | 2 | |
|3 | | 1 |
currently, i can only output 2 columns, (ID and Absent) or (ID and Late) using this code:
目前,我只能使用以下代码输出2列(ID和Absent)或(ID和Late):
SELECT id, count(remarks) AS Absent
FROM attendance
WHERE remarks = 'Absent'
GROUP BY id;
i can't display absent and late column simultaneously.. please help. thanks.
我不能同时显示缺席和晚期列..请帮忙。谢谢。
3 个解决方案
#1
2
This is basically a PIVOT
. If you do not have access to a PIVOT
function then, you can replicate it with an aggregate function and a CASE
statement:
这基本上是一个PIVOT。如果您无权访问PIVOT函数,则可以使用聚合函数和CASE语句复制它:
select id,
sum(case when remarks = 'Absent' then 1 else 0 end) Absent,
sum(case when remarks = 'Late' then 1 else 0 end) Late
from attendance
group by id
请参阅SQL Fiddle with Demo
Or you can use COUNT()
:
或者您可以使用COUNT():
select id,
count(case when remarks = 'Absent' then 1 else null end) Absent,
count(case when remarks = 'Late' then 1 else null end) Late
from attendance
group by id;
请参阅SQL Fiddle with Demo
#2
0
try:
SELECT id,
Sum (Case remarks When 'Absent' Then 1 End) Absent,
Sum (Case remarks When 'Late' Then 1 End) Late
FROM attendance
GROUP BY id;
#3
0
Use a SUM(CASE)
construct to separate the Absent
and Late
. For each one, the CASE returns a 1 or 0 if the value is matched, and then those are added up via the aggregate SUM()
giving the total number. The concept at work here is known as a pivot table.
使用SUM(CASE)构造来分离Absent和Late。对于每一个,如果值匹配,则CASE返回1或0,然后通过汇总SUM()给出总数。这里的工作概念称为数据透视表。
SELECT
id,
SUM(CASE WHEN Remarks = 'Absent' THEN 1 ELSE 0 END) AS Absent,
SUM(CASE WHEN Remarks = 'Late' THEN 1 ELSE 0 END) AS Late
FROM
attendance
GROUP BY id
#1
2
This is basically a PIVOT
. If you do not have access to a PIVOT
function then, you can replicate it with an aggregate function and a CASE
statement:
这基本上是一个PIVOT。如果您无权访问PIVOT函数,则可以使用聚合函数和CASE语句复制它:
select id,
sum(case when remarks = 'Absent' then 1 else 0 end) Absent,
sum(case when remarks = 'Late' then 1 else 0 end) Late
from attendance
group by id
请参阅SQL Fiddle with Demo
Or you can use COUNT()
:
或者您可以使用COUNT():
select id,
count(case when remarks = 'Absent' then 1 else null end) Absent,
count(case when remarks = 'Late' then 1 else null end) Late
from attendance
group by id;
请参阅SQL Fiddle with Demo
#2
0
try:
SELECT id,
Sum (Case remarks When 'Absent' Then 1 End) Absent,
Sum (Case remarks When 'Late' Then 1 End) Late
FROM attendance
GROUP BY id;
#3
0
Use a SUM(CASE)
construct to separate the Absent
and Late
. For each one, the CASE returns a 1 or 0 if the value is matched, and then those are added up via the aggregate SUM()
giving the total number. The concept at work here is known as a pivot table.
使用SUM(CASE)构造来分离Absent和Late。对于每一个,如果值匹配,则CASE返回1或0,然后通过汇总SUM()给出总数。这里的工作概念称为数据透视表。
SELECT
id,
SUM(CASE WHEN Remarks = 'Absent' THEN 1 ELSE 0 END) AS Absent,
SUM(CASE WHEN Remarks = 'Late' THEN 1 ELSE 0 END) AS Late
FROM
attendance
GROUP BY id