SQL Query按月获得每个卖家的总销售额

时间:2013-11-01 14:09:16

标签: mysql sql select

我试试这个:

SELECT
  profile.user_id,
  profile.name,
  total_month.total as month10
FROM profile
  LEFT OUTER JOIN (SELECT
      order.seller_id,
      COUNT(*) AS total
    FROM order
    WHERE MONTH(order.data_hora) = 10
    GROUP BY order.seller_id) AS total_month
    ON total_month.seller_id= profile.user_id;

结果如下:

-------------------------
|user_id| name  |month10|
-------------------------
|   5   |user1  |  73   |
|   1   |user2  |   1   |
-------------------------

但我需要更多这样的月份:

-------------------------------------------------
| user_id | name  | month10 | month11 | month12 |
-------------------------------------------------
|    5    | user1 |   73    |    52   |    65   |
|    1    | user2 |   67    |    56   |    78   |
-------------------------------------------------

如果不创建函数,我怎么能这样做?

3 个答案:

答案 0 :(得分:2)

您可以扩展查询以执行所需操作。在子查询中更灵活:

SELECT p.user_id, p.name,
       tm.month10, tm.month11, tm.month12
FROM profile p LEFT OUTER JOIN
     (SELECT o.seller_id,
             sum(o.data_hora = 10) as month10,
             sum(o.data_hora = 11) as month11,
             sum(o.data_hora = 12) as month12
      FROM order o
      WHERE MONTH(order.data_hora) in (10, 11, 12)
      GROUP BY order.seller_id
     ) tm
     ON tm.seller_id = p.user_id;

答案 1 :(得分:0)

正如我在支持@Schalk评论的评论中所说的那样,为了让你的工作正常,你需要一个函数来获得DYNAMIC PIVOT TABLETRANSPOSE ROWS TO COLUMNS google它。

对于您的解决方案,我创建了一个查询,为您提供所有月份/值:

user_id  name  jan feb mar  apr  may jun jul aug sep oct nov dec
   1       a    0   0   1   10    0   0   1   2   7   2   3   0 
   2       b    1   0   0    0    2   0   0   0   3   1   1   0 

如果它适合您的问题,这是您的查询:

select
    profile.user_id,
    profile.name,
    sum(if( MONTH(orderr.data_hora) = 1, 1, 0 )) as Jan,
    sum(if( MONTH(orderr.data_hora) = 2, 1, 0 )) as Feb,
    sum(if( MONTH(orderr.data_hora) = 3, 1, 0 )) as Mar,
    sum(if( MONTH(orderr.data_hora) = 4, 1, 0 )) as Apr,
    sum(if( MONTH(orderr.data_hora) = 5, 1, 0 )) as May,
    sum(if( MONTH(orderr.data_hora) = 6, 1, 0 )) as Jun,
    sum(if( MONTH(orderr.data_hora) = 7, 1, 0 )) as Jul,
    sum(if( MONTH(orderr.data_hora) = 8, 1, 0 )) as Aug,
    sum(if( MONTH(orderr.data_hora) = 9, 1, 0 )) as Sep,
    sum(if( MONTH(orderr.data_hora) = 10, 1, 0 )) as Oct,
    sum(if( MONTH(orderr.data_hora) = 11, 1, 0 )) as Nov,
    sum(if( MONTH(orderr.data_hora) = 12, 1, 0 )) as Dece
from 
    profile left join orderr 
                   on profile.user_id = orderr.seller_id
group by profile.user_id,
         profile.name

我已经创建了一个小提琴(但我创建的data_hora列为整数,以便快速完成它,这是为了理解)。

http://sqlfiddle.com/#!2/4a1a2e/5

答案 2 :(得分:0)

很完美!

字段类型'data_hora'是日期时间,所以我做了一个小改动。

SELECT p.user_id,
       p.name,
       tm.month10,
       tm.month11,
       tm.month12,
       (tm.month10+tm.month11+tm.month12) AS final_total
FROM profile p
LEFT OUTER JOIN
  (SELECT o.seller_id,
          sum(month(o.data_hora) = 10) AS month10,
          sum(month(o.data_hora) = 11) AS month11,
          sum(month(o.data_hora) = 12) AS month12
   FROM order o
   WHERE MONTH(ORDER.data_hora) IN (10, 11, 12)
   GROUP BY ORDER.seller_id ) tm 
   ON tm.seller_id = p.user_id
ORDER BY final_total DESC;

我如何优化字段“final_total”?