Mysql 对满足一定条件的 计数 常用的一条sql

来源:互联网 发布:学服装设计的软件 编辑:程序博客网 时间:2024/06/07 05:34

SELECT ord_city_id , ord_model_id, count(distinct case when match_dis<2 then ord_id else null end ) as 2gongli , count(DISTINCT ord_id) as dingdantotal
from spipei group by ord_city_id , ord_model_id

解释:
对spipei表按ord_city_id , ord_model_id分组统计如下信息:
1, 订单总数 : dingdantotal
2,满足match_dis小于2Km的订单数: 2gongli

其中,count(distinct case when match_dis<2 then ord_id else null end ) 比较好用

0 0
原创粉丝点击