sql语句查询字段不同取值对应的不同总数

sql语句查询字段不同取值对应的不同总数#

需求:数据库字段userType可能为0也可能为1,那现在要在sql里面统计每天的userType为0的总数和为1的总数:
①返回每天的日期格式为yyyy-MM-dd所以要用DATE_FORMAT(create_time,’%Y-%m-%d’)
②去重,以天为单位统计DISTINCT()
③userType为0,1的,分别统计统计:
count(case when userType = 0 then userType = 0 end) userCount,
count(case when userType = 1 then userType = 1 end) companyCount
④按时间分类(每天)GROUP BY dateTime
SELECT DISTINCT(DATE_FORMAT(create_time,’%Y-%m-%d’)) dateTime,
count(case when userType = 0 then userType = 0 end) userCount,
count(case when userType = 1 then userType = 1 end) companyCount
FROM sys_user
WHERE
1=1
AND status = 1
GROUP BY dateTime
ORDER BY dateTime DESC

你可能感兴趣的:(java)