业务场景:现有需求需要按时间统计所有订单和所有成功订单,查询条件为扩展字段一。(status=2代表成功)。
sql:
SELECT
c_DATE,
COUNT(success) AS sucess,
COUNT(*) AS total
FROM
(SELECT
DATE_FORMAT(CREATE_DATE, '%Y-%m-%d') AS c_DATE,
IF(state = "2", TRUE, NULL) AS success
FROM
mkt_s.mkt_order_201909
WHERE EXT1 = "116404") t
GROUP BY t.c_DATE;
查询结果:
总结:count(success)统计子查询中成功的条数,count(0)或者count(1)统计所有条目。