Re: [SQL ] 同一表格同一欄位資料統計

看板Database作者 (rock)時間12年前 (2013/11/22 00:16), 編輯推噓2(200)
留言2則, 2人參與, 最新討論串6/6 (看更多)
※ 引述《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 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.136.224.65

11/22 11:57, , 1F
哇,原來 count 裡面可以用「表示式」來代表要不要 count
11/22 11:57, 1F

11/22 16:32, , 2F
受教
11/22 16:32, 2F
文章代碼(AID): #1IZZ7YU3 (Database)
討論串 (同標題文章)
文章代碼(AID): #1IZZ7YU3 (Database)