MySQL逐个月地返回每个不同ID的计数

时间:2017-01-13 18:17:39

标签: mysql

我编写了下面的查询,它有效,但效率极低且速度慢。我该怎么做才能提高效率?我知道它基本上是“循环”系统中的每个ID,其中大约有500个,每个ID运行13个选择语句 - 我只是不知道更好的方法来获取这些信息。

我的日期字段是<ul class="nav navbar-nav" id="navbar-ul"> <navmenu v-for="item in navlist" v-bind:menu="item"> <navmenu-dropdown v-if="item.class === 'dropdown'" v-bind:dropdownlist="item"> <navmenu-dropdown-li v-for="itemli in item.menus" v-bind:dropdownmenu="itemli"></navmenu-dropdown-li> </navmenu-dropdown> </navmenu> </ul> 字段,存储为DATE

YYYY-MM-DD

4 个答案:

答案 0 :(得分:1)

只按年,月和身份分组

select id, sum(captured_profit), month( `date`)
from me 
where id is not null and `date` > '2016-12-31'
group by year(`date`), month(`date`), id;

我猜你没有使用所有的关键字(名称,日期)作为实际的列名,如果你确实在每个查询中都将它们放在反引号中。

答案 1 :(得分:1)

SUM()等聚合函数忽略NULL。因此,使用表达式将利润值设为NULL,除非它在您想要的范围内。

SELECT 
  id AS name,
  SUM(CASE WHEN MONTH(date) = 1 THEN captured_profit END) AS January,
  SUM(CASE WHEN MONTH(date) = 2 THEN captured_profit END) AS February,
  SUM(CASE WHEN MONTH(date) = 3 THEN captured_profit END) AS March,
  SUM(CASE WHEN MONTH(date) = 4 THEN captured_profit END) AS April,
  SUM(CASE WHEN MONTH(date) = 5 THEN captured_profit END) AS May,
  SUM(CASE WHEN MONTH(date) = 6 THEN captured_profit END) AS June,
  SUM(CASE WHEN MONTH(date) = 7 THEN captured_profit END) AS July,
  SUM(CASE WHEN MONTH(date) = 8 THEN captured_profit END) AS August,
  SUM(CASE WHEN MONTH(date) = 9 THEN captured_profit END) AS September,
  SUM(CASE WHEN MONTH(date) = 10 THEN captured_profit END) AS October,
  SUM(CASE WHEN MONTH(date) = 11 THEN captured_profit END) AS November,
  SUM(CASE WHEN MONTH(date) = 12 THEN captured_profit END) AS December,
  SUM(captured_profit) AS Total
FROM me
WHERE id IS NOT NULL AND YEAR(date) = 2017
GROUP BY id;

如果没有匹配且没有CASE子句,则ELSE表达式返回NULL。请阅读https://dev.mysql.com/doc/refman/5.7/en/case.html了解详情。

此查询消除了所有相关子查询。查询将只扫描一次表。

我不知道您的“总计”的含义在您的查询中是什么,但它似乎只是11月+ 12月。我不确定这是不是你想要的。

编辑:我修改了Total的表达式。

答案 2 :(得分:1)

Bill Karwin打败了我,但我还添加了按年份分组的总和:     

SELECT 
    id as name,
    SUM(CASE WHEN MONTH(date) = 1 THEN captured_profit ELSE 0 END) AS January,
    SUM(CASE WHEN MONTH(date) = 2 THEN captured_profit ELSE 0 END) AS February,
    SUM(CASE WHEN MONTH(date) = 3 THEN captured_profit ELSE 0 END) AS March,
    SUM(CASE WHEN MONTH(date) = 4 THEN captured_profit ELSE 0 END) AS April,
    SUM(CASE WHEN MONTH(date) = 5 THEN captured_profit ELSE 0 END) AS May,
    SUM(CASE WHEN MONTH(date) = 6 THEN captured_profit ELSE 0 END) AS June,
    SUM(CASE WHEN MONTH(date) = 7 THEN captured_profit ELSE 0 END) AS July,
    SUM(CASE WHEN MONTH(date) = 8 THEN captured_profit ELSE 0 END) AS August,
    SUM(CASE WHEN MONTH(date) = 9 THEN captured_profit ELSE 0 END) AS September,
    SUM(CASE WHEN MONTH(date) = 10 THEN captured_profit ELSE 0 END) AS October,
    SUM(CASE WHEN MONTH(date) = 11 THEN captured_profit ELSE 0 END) AS November,
    SUM(CASE WHEN MONTH(date) = 12 THEN captured_profit ELSE 0 END) AS December,
    SUM(captured_profit) AS Total,
    YEAR(date)
from me
where id is not null
group by id, YEAR(date)
union
SELECT 
    NULL,
    SUM(CASE WHEN MONTH(date) = 1 THEN captured_profit ELSE 0 END) AS January,
    SUM(CASE WHEN MONTH(date) = 2 THEN captured_profit ELSE 0 END) AS February,
    SUM(CASE WHEN MONTH(date) = 3 THEN captured_profit ELSE 0 END) AS March,
    SUM(CASE WHEN MONTH(date) = 4 THEN captured_profit ELSE 0 END) AS April,
    SUM(CASE WHEN MONTH(date) = 5 THEN captured_profit ELSE 0 END) AS May,
    SUM(CASE WHEN MONTH(date) = 6 THEN captured_profit ELSE 0 END) AS June,
    SUM(CASE WHEN MONTH(date) = 7 THEN captured_profit ELSE 0 END) AS July,
    SUM(CASE WHEN MONTH(date) = 8 THEN captured_profit ELSE 0 END) AS August,
    SUM(CASE WHEN MONTH(date) = 9 THEN captured_profit ELSE 0 END) AS September,
    SUM(CASE WHEN MONTH(date) = 10 THEN captured_profit ELSE 0 END) AS October,
    SUM(CASE WHEN MONTH(date) = 11 THEN captured_profit ELSE 0 END) AS November,
    SUM(CASE WHEN MONTH(date) = 12 THEN captured_profit ELSE 0 END) AS December,
    SUM(captured_profit) AS Total,
    YEAR(date)
from me
where id is not null
group by YEAR(date)

答案 3 :(得分:0)

我认为这正是您所寻找的

http://www.w3resource.com/mysql/date-and-time-functions/mysql-monthname-function.php

MySQL MONTHNAME()返回给定日期的月份的全名。返回值在1到12(1月到12月)的范围内。当日期的月份部分为0或大于12时,它返回NULL

语法:

MONTHNAME(date1)