统计并合并同一张表中不同条件的数据

需求

统计某一天内,启动应用的设备总数,以及不同渠道来源的设备数量。

sql

数据库是mysql


SET @date = 'yyyy-MM-dd%';

SELECT
	COUNT(
		DISTINCT CASE
		WHEN create_date LIKE @date THEN
			imei
		END
	) AS total,
	COUNT(
		DISTINCT CASE
		WHEN create_date LIKE @date
		AND channel = 'google' THEN
			imei
		END
	) AS update_dialog,
	COUNT(
		DISTINCT CASE
		WHEN create_date LIKE @date
		AND channel = 'qq' THEN
			imei
		END
	) AS update_now
FROM
	t_app_download
WHERE
	appid= "com.app.test"
posted @ 2017-09-11 22:10  月半流云  阅读(559)  评论(0编辑  收藏  举报