Re: [SQL ] 同一表格同一字段资料统计

楼主: rockchangnew (rock)   2013-11-22 00:16:32
※ 引述《joedenkidd (优质的蓝色射手)》之铭言:
: ※ 引述《qwertmn (抽筋)》之铭言:
: : select
: : count(if(finish=1, true, null)) as f1,
: : count(if(finish=0, true, null)) as f0;
: : 这样?
: cutekid大大的方法,可以用,但因为我叙述不清,所以不适用,歹势
: select finish,sum(*) from yield_process where finish=0 or finish=1 group by fi
: nish
: qwertmn大大的方法我试了,无法执行,不知是因为pgsql不支援或是其他原因
: 但是,我发现我发问的问题有瑕疵,不好意思
: finish 的型态是 character,记录的是完成时间
: class是品名,盘子、杯子.....
: 所以我都用最笨的方式分两次寻找
: select sum(*) from table where finish !='' group by class //当完成的时候,这
: 一栏不会是空的
: select sun(*) from table where finish='' group by class //当完成的时候,这一栏
: 会是空的
: 所以我想说要将这两个整合成一个,显示结果如下
: 品名 完成 未完成
: 盘子 6 3
: 杯子 8 4
: 不知道有没有办法用一个语法就完成这样的一件事?
select class as 品名,
count(case when finish <>'' then '1' else null end) as 完成,
count(case when finish ='' then '1' else null end) as 未完成,
from table
group by class
作者: cutekid (可爱小孩子)   2012-01-22 11:57:00
哇,原来 count 里面可以用“表示式”来代表要不要 count
作者: joedenkidd (优质的蓝色射手)   2012-01-22 16:32:00
受教

Links booklink

Contact Us: admin [ a t ] ucptt.com