根據條件,分別累加,達到計數目的co
sum(case when status=1 then 1 else 0 end) 推薦此語句cas
sum(case when status=2 then 1 else 0 end)錯誤
sum(case when status=3 then 1 else 0 end)
下面語句是錯誤的:
count(case when status=1 then 1 else 0 end)
count(case when status=2 then 1 else 0 end)
count(case when status=3 then 1 else 0 end)
三條語句,結果相同,主要是由於count0 count1結果相同
修改成 count(case when status=1 then orderid end)
不推薦此用法,理由以下:通常if-else 配對出現。