首页 > 技术文章 > 二维GROUP BY

xiaoheshang 2015-06-10 14:47 原文

上午参加了个计算机英语三级考试,回来后BA同事让帮忙统计数据。可能刚考完试思维比较混乱, 整理了好大一会没有想明白怎么写。

最后挣扎了快一个小时终于想起来,记下来留个备份;

select store_no, brand_code, count(distinct t.dealer_code)
          from juran.emall_booth_contract t
         group by store_no, brand_code
        having count(distinct t.dealer_code) > 1

如果统计表数量则是:selelct count(*) from emall_booth_contract;

如果统计门店数量:select store_no,count(*) from emall_booth_contract;

统计门店数量大于1:select store_no from emall_booth_contract group by store_no having count(*) >1;

当时怎么想不明白怎么统计门店下品牌数量,其实select两列的话,是按照那两列分组,统计没组下有多少个品牌;

加上distinct进行重复值去除。

having条件是把每组下数量大于1的结果查出;

推荐阅读