扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
select 多条记录如何合并为一条记录?
select studentid,resourceTypeID,resourceCount from studentResourceAccept
结果: studentid resourceTypeID resourceCount (sfz,身份证,zzz,暂住证,zp,照片,wjz外交正,jgz,军官证)
240011 sfz 1
240011 zp 8
240011 zzz 1
250011 wjz 1
250011 jgz 1
想要的结果: studentid TypeID1 Count1 TypeID2 Count2 TypeID3 Count3 typeID4 count4 typeid5 count5
240011 sfz 1 zp 8 zzz 1
250011 wjz 1 jgz 1
sql语句如何写?
SQL codeselect studentid,
sum(case resourceTypeID when 'sfz' then 1 else 0 end) sfz,
sum(case resourceTypeID when 'zzz' then 1 else 0 end) zzz,
sum(case resourceTypeID when 'zp' then 1 else 0 end) zp,
sum(case resourceTypeID when 'wjz' then 1 else 0 end) wjz,
sum(case resourceTypeID when 'jgz' then 1 else 0 end) jgz
from tb
group by studentid
create table ttt(studentid varchar2(20),resourcetypeid varchar2(10),resourcecount int);
insert into ttt values('240011','sfz',1);
insert into ttt values('240011','zp',8);
insert into ttt values('240011','zzz',1);
insert into ttt values('250011','wjz',1);
insert into ttt values('250011','jgz',1);
select studentid,
'sfz',sum(decode(resourcetypeid,'sfz',resourcecount,0)) count1,
'zzz',sum(decode(resourcetypeid,'zzz',resourcecount,0)) count2,
'zp',sum(decode(resourcetypeid,'zp',resourcecount,0)) count3,
'wjz',sum(decode(resourcetypeid,'wjz',resourcecount,0)) count4,
'jgz',sum(decode(resourcetypeid,'jgz',resourcecount,0)) count5
from
ttt
group by studentid
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者