我有sql表,如图所示
Name |eng |kisw
Jane |A |B
Ann |B |B
Mag |B |B
我需要计算Eng A=1
B=2
和Kisw B=3
我有sql代码,但显示错误的数据
SELECT name,COUNT(eng),COUNT(kisw) FROM form1 GROUP BY eng ,kisw
答案 0 :(得分:1)
我想,这会有所帮助:
SELECT 'ENG' as SUBJECT, ENG AS GRADE, COUNT(ENG) AS GRADE_COUNT FROM YOUR_TABLE GROUP BY ENG
UNION
SELECT 'KISW' AS SUBJECT, KISW AS GRADE, COUNT(KISW) AS GRADE_COUNT FROM YOUR_TABLE GROUP BY KISW
输出是:
ENG |A |1
ENG |B |2
KISW |B |3
运行测试查询使用:
WITH TEST_DATA AS
(
SELECT 'JANE' NAME, 'A' ENG, 'B' KISW FROM DUAL
UNION
SELECT 'ANN' NAME, 'B' ENG, 'B' KISW FROM DUAL
UNION
SELECT 'MAG' NAME, 'B' ENG, 'B' KISW FROM DUAL
)
SELECT 'ENG' SUBJECT, ENG GRADE, COUNT(ENG) GRADE_COUNT FROM TEST_DATA GROUP BY ENG
UNION
SELECT 'KISW' SUBJECT, KISW GRADE, COUNT(KISW) GRADE_COUNT FROM TEST_DATA GROUP BY KISW
答案 1 :(得分:0)
尝试这是有帮助的
select name,
sum(eng = 'A') totalA,
sum(eng= 'B') totalB,
sum(eng = 'C') totalC
from student
group by name
了解更多详情,请访问此