如何从每个类别获得前40%的销售贡献者?

时间:2015-01-12 06:20:30

标签: mysql sql

我有一个包含3列的表:Category,Seller,Sales

catgeory  seller    Sales
a     ab        1654
a     bc        1656
a     cd        1136
a     da        1421
a     ef        1309
a     gh        1624
b     cd        1754
b     hj        1542
b     kl        1925
b     mn        1915
c     op        1912
c     ab        1335
c     de        1079
c     pq        1316
d     ag        1890
d     df        1817

我只想要在每个类别中贡献前40%销售额的卖家列表。使用Mysql查询。任何想法?

2 个答案:

答案 0 :(得分:2)

我认为这将是这样的:

select seller
from table
where sales IN (
    select max(sales)
    from table
    group by category)

答案 1 :(得分:1)

请考虑以下事项。此架构假设PK(类别,卖方)......

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(category CHAR(1) NOT NULL
,seller    CHAR(2) NOT NULL
,Sales INT NOT NULL
,PRIMARY KEY(category,seller)
);

INSERT INTO my_table VALUES
('a'  ,   'ab'   ,     1654),
('a'  ,   'bc'   ,     1656),
('a'  ,   'cd'   ,     1136),
('a'  ,   'da'   ,     1421),
('a'  ,   'ef'   ,     1309),
('a'  ,   'gh'   ,     1624),
('b'  ,   'cd'   ,     1754),
('b'  ,   'hj'   ,     1542),
('b'  ,   'kl'   ,     1925),
('b'  ,   'mn'   ,     1915),
('c'  ,   'op'   ,     1912),
('c'  ,   'ab'   ,     1335),
('c'  ,   'de'   ,     1079),
('c'  ,   'pq'   ,     1316),
('d'  ,   'ag'   ,     1890),
('d'  ,   'df'   ,     1817);

这是一个中间解决方案。它可以让您获得每个卖家的百分比贡献。

SELECT a.category
     , a.seller
     , a.sales
     , IF(category = @prev,@pct := @pct + a.pct,@pct:=pct) total
     , @prev := category
  FROM
     ( SELECT x.*
            , SUM(DISTINCT x.sales)/SUM(y.sales) pct 
         FROM my_table x 
         JOIN my_table y 
           ON y.category = x.category 
        GROUP 
           BY x.category
            , x.seller
     ) a
     , (SELECT @pct := 0,@prev:='') vars
 ORDER 
    BY a.category
     , a.pct DESC;
+----------+--------+-------+--------+-------------------+
| category | seller | sales | total  | @prev := category |
+----------+--------+-------+--------+-------------------+
| a        | bc     |  1656 | 0.1882 | a                 |<--
| a        | ab     |  1654 | 0.3762 | a                 |<-- Desired users
| a        | gh     |  1624 | 0.5607 | a                 |<--
| a        | da     |  1421 | 0.7222 | a                 |
| a        | ef     |  1309 | 0.8710 | a                 |
| a        | cd     |  1136 | 1.0001 | a                 |
| b        | kl     |  1925 | 0.2698 | b                 |<--
| b        | mn     |  1915 | 0.5382 | b                 |<--
| b        | cd     |  1754 | 0.7840 | b                 |
| b        | hj     |  1542 | 1.0001 | b                 |
| c        | op     |  1912 | 0.3389 | c                 |<--
| c        | ab     |  1335 | 0.5755 | c                 |<--
| c        | pq     |  1316 | 0.8088 | c                 |
| c        | de     |  1079 | 1.0000 | c                 |
| d        | ag     |  1890 | 0.5098 | d                 |<--
| d        | df     |  1817 | 1.0000 | d                 |
+----------+--------+-------+--------+-------------------+

完整的解决方案通过对无法贡献60%的用户进行折扣来扭转逻辑......

SELECT n.category
     , n.seller
     , n.sales
  FROM 
     ( SELECT a.category
     , a.seller
     , a.sales
     , IF(category = @prev,@pct := @pct + a.pct,@pct:=pct) total
     , @prev := category
  FROM
     ( SELECT x.*
            , SUM(DISTINCT x.sales)/SUM(y.sales) pct 
         FROM my_table x 
         JOIN my_table y 
           ON y.category = x.category 
        GROUP 
           BY x.category
            , x.seller
     ) a
     , (SELECT @pct := 0,@prev:='') vars
 ORDER 
    BY a.category
     , a.pct                         -- <-- reversed
     ) n
   WHERE total >= 0.6
  ORDER BY category,sales DESC;
+----------+--------+-------+
| category | seller | sales |
+----------+--------+-------+
| a        | bc     |  1656 |
| a        | ab     |  1654 |
| a        | gh     |  1624 |
| b        | kl     |  1925 |
| b        | mn     |  1915 |
| c        | op     |  1912 |
| c        | ab     |  1335 |
| d        | ag     |  1890 |
+----------+--------+-------+