节前面试给了一道题
表1
id status2 status3
1 F Y
1 F X
2 X X
实际数据不止3条,我截取了一部分,要求查询当ID为1时,status2为F的有几个,status3为Y的有几个,
最后的结果为:
1 2 1
要求用sql语句写。。。。
------解决方案--------------------------------------------------------
- SQL code
select id,sum(decode(status2,'F',1)),sum(decode(status3,'Y',1))from 表1where id=1group by id
------解决方案--------------------------------------------------------
典型的行转列统计
------解决方案--------------------------------------------------------
- SQL code
select id,count(decode(status2,'F',1)),count(decode(status3,'Y',1)) from tb where id=1;
------解决方案--------------------------------------------------------
这里主要考的就是字符函数decode的运用+行`列统计
------解决方案--------------------------------------------------------
1楼正解, 3楼缺少GROUP BY ID