看板 Database 關於我們 聯絡資訊
select coalesce(t1.class,t2.class), coalesce(t1.cnt,0), coalesce(t2.cnt,0) from ( select class, count(*) as cnt from table where finish != '' group by class ) t1 full outer join ( select class, count(*) as cnt from table where finish = '' group by class ) t2 on t1.class = t2.class 其中 coalesce 參考: http://www.postgresql.org/docs/8.4/static/functions-conditional.html ※ 引述《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 : 不知道有沒有辦法用一個語法就完成這樣的一件事? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.170.28.144
joedenkidd:感謝大大,我試了一下,可以解決我的問題,大力感謝! 11/21 16:35