mysql group by xx top N

今天 运营又要数据;

            

1、每个产品的品牌、货号、规格、单位、单价、销售总数量、销售总额、销售订单数、采购实验室数、供货商(销售金额最高的3个商家);
2、品牌、货号相同,即为一个产品;
3、包含线上与线下数据;
4、2015年与2016年分开统计。

 前面几个数据还好 就是  供货商(销售金额最高的3个商家) 这个不好整

我的思路是前面的抽出 一张表 A表(商品id ……)

后面的抽出一张表 B表(供应商id ,总价格,商品id )

create table temp_suppler_key1 as
 SELECT t.fkey,  
 substring_index( group_concat( IFNULL(t.`name`,0) ORDER BY t.tprice DESC ), ",", 3 ) `name` 

FROM temp_suppler_key  t GROUP BY t.fkey  ;

1、先group_concat 排序 然后在substring 比较不错


网上有大神这样解决的感觉不错

http://www.wfuyu.com/datay/21132.html

依照a,b分组,并且依照itime字段倒叙排列,取每组的top3

SELECT t.a, t.b, substring_index( group_concat( IFNULL(t.c,0) ORDER BY t.itime DESC ), ",", 3 ) c, substring_index( group_concat( t.itime ORDER BY t.itime DESC ), ",", 3 ) time FROM t t GROUP BY t.a ,t.b;

猜你喜欢

转载自blog.csdn.net/oJAVAc/article/details/80804607
今日推荐