17,137
社区成员
发帖
与我相关
我的任务
分享create table tb(student_id varchar2(10),testpaper_id int,title_id int,content varchar2(10))
insert into tb values('00001',60,1,'A')
insert into tb values('00001',60,2,'AB')
insert into tb values('00001',60,3,'C')
insert into tb values('00001',60,4,'ABD')
insert into tb values('00001',60,5,'B')
insert into tb values('00002',60,1,'C')
insert into tb values('00002',60,2,'ABC')
insert into tb values('00002',60,3,'D')
insert into tb values('00002',60,4,'A')
insert into tb values('00002',60,5,'BD')
insert into tb values('00003',60,1,'D')
insert into tb values('00003',60,2,'ABC')
insert into tb values('00003',60,3,'AD')
insert into tb values('00003',60,4,'A')
insert into tb values('00003',60,5,'C')
select testpaper_id,title_id,
sum(case when instr(content,'A',1,1) > 0 then 1 else 0 end) A,
sum(case when instr(content,'B',1,1) > 0 then 1 else 0 end) B,
sum(case when instr(content,'C',1,1) > 0 then 1 else 0 end) C,
sum(case when instr(content,'D',1,1) > 0 then 1 else 0 end) D
from tb
group by testpaper_id,title_id
drop table tb
/*
TESTPAPER_ID TITLE_ID A B C D
------------ ---------- ---------- ---------- ---------- ----------
60 1 1 0 1 1
60 2 3 3 2 0
60 3 1 0 1 2
60 4 3 1 0 1
60 5 0 2 1 1
5 rows selected.
*/
create table tb(student_id varchar2(10),testpaper_id int,title_id int,content varchar2(10))
insert into tb values('00001',60,1,'A')
insert into tb values('00001',60,2,'AB')
insert into tb values('00001',60,3,'C')
insert into tb values('00001',60,4,'ABD')
insert into tb values('00001',60,5,'B')
insert into tb values('00002',60,1,'C')
insert into tb values('00002',60,2,'ABC')
insert into tb values('00002',60,3,'D')
insert into tb values('00002',60,4,'A')
insert into tb values('00002',60,5,'BD')
insert into tb values('00003',60,1,'D')
insert into tb values('00003',60,2,'ABC')
insert into tb values('00003',60,3,'AD')
insert into tb values('00003',60,4,'A')
insert into tb values('00003',60,5,'C')
select testpaper_id,title_id,
sum(case when instr(content,'A',1,1) > 0 then 1 else 0 end) A,
sum(case when instr(content,'B',1,1) > 0 then 1 else 0 end) B,
sum(case when instr(content,'C',1,1) > 0 then 1 else 0 end) C,
sum(case when instr(content,'D',1,1) > 0 then 1 else 0 end) D
from tb
group by testpaper_id,title_id
drop table tb
/*
select testpaper_id,title_id,
sum(case when instr(content,'A',1,1) > 0 then 1 else 0 end) A,
sum(case when instr(content,'B',1,1) > 0 then 1 else 0 end) B,
sum(case when instr(content,'C',1,1) > 0 then 1 else 0 end) C,
sum(case when instr(content,'D',1,1) > 0 then 1 else 0 end) D
from kst
group by testpaper_id,title_id
*/
select testpaper_id,title_id,
sum(case when instr(content,'A',1,1) > 0 then 1 else 0 end) A,
sum(case when instr(content,'B',1,1) > 0 then 1 else 0 end) B,
sum(case when instr(content,'C',1,1) > 0 then 1 else 0 end) C,
sum(case when instr(content,'D',1,1) > 0 then 1 else 0 end) D
from kst
group by testpaper_id,title_id