0

我试试这个:

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   |
-------------------------------------------------

如果不创建函数,我怎么能做到这一点?

4

3 回答 3

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;
于 2013-11-01T14:59:48.667 回答
0

正如我在支持@Schalk 评论的评论中所说的那样,为了让这个工作如你所愿,如果你愿意,你需要一个函数来获取DYNAMIC PIVOT TABLETRANSPOSE ROWS TO COLUMNS谷歌它。

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

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

于 2013-11-01T16:44:14.757 回答
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”字段?

于 2013-11-01T16:55:44.430 回答