---
title: "count()函数里面想实现带条件统计，可以使用SUM函数来进行替换"
author: "aaron7524"
date: "2013-07-10T17:14:17Z"
tags: ["Case When", "decode", "sum"]
categories: ["数据库"]
slug: "count-sum"
draft: false
lastmod: "2013-07-10T17:14:17Z"
id: 1902671
url: "https://aaron7524.iteye.com/blog/1902671"
---

count()函数里面想实现带条件统计，可以使用SUM函数来进行替换，具体可以使用case when 语句或者Decode函数来对要统计的数据进行0、1转换，


如下例子：
SELECT s.user_id,SUM(case(m.is_success) when 1 then 1 else 0 end),SUM(case when m.read_time IS NULL  then 0 else 1 end),COUNT(*)
	from doc_score s,mail_send_log m where s.user_id in(6,7,8,9) and s.status = 1 and m.doc_id = s.doc_id 
	group by s.user_id
;