当前位置: 代码迷 >> Oracle开发 >> 请问一个SQL有关问题.(全部分,送给大家)
  详细解决方案

请问一个SQL有关问题.(全部分,送给大家)

热度:97   发布时间:2016-04-24 07:01:04.0
请教一个SQL问题.(全部分,送给大家)
本帖最后由 s63403048 于 2013-05-17 09:40:25 编辑
表xxx中有trueandfalse这个字段
当为0时则是真,
当为1时则是假,
现在需要.
同时求出真的数量,以及假的数量.
此SQL应如何编写呢.
SQL

------解决方案--------------------
哈哈,所以还是自己多思考会,说不定就想出来了。
可惜你写的还是有问题,如果为假,你得到的结果还是0,sum(0)=0,应该这样写:
select sum(case
             when car.trueandfalse = 0 then
              1
           end) as realsum,
       sum(case
             when car.trueandfalse = 1 then
              1
           end) as falsesum
  from bascarinfo car




一般 的都会写成和楼主一样的,还可以这样写
select (select count(1) from bascarinfo where trueandfalse = 0) realsum,
       (select count(1) from bascarinfo where trueandfalse = 1) falsesum
  from dual;

------解决方案--------------------
解决就行 结贴给分 
------解决方案--------------------
SELECT SUM(DECODE(trueandfalse,1,1,0))  T1,SUM(DECODE(trueandfalse,0,1,0)) t2 FROM XXX; 
  相关解决方案