MySQL:一行多次计数

时间:2016-06-06 08:07:12

标签: mysql sql

我有下一个数据:

mysql> select no,crt_date,tobilling_date,sent_to_client,dop_prov from assistfin limit 20;
+--------+---------------------+---------------------+----------------+------------+
| no     | crt_date            | tobilling_date      | sent_to_client | dop_prov   |
+--------+---------------------+---------------------+----------------+------------+
|  50.01 | 2014-02-05 10:28:10 | 2014-02-05 14:42:35 | 2014-04-16     | 2014-09-23 |
| 123.01 | 2014-02-05 19:17:36 | 2014-03-17 18:58:05 | 2014-04-10     | 2014-06-30 |
|  51.01 | 2014-02-06 00:09:32 | 2014-03-20 16:53:46 | 2014-04-10     | 2014-06-30 |
| 124.01 | 2014-02-06 15:29:08 | 2014-03-20 17:04:42 | 2014-04-10     | 2014-06-30 |
| 230.01 | 2014-02-07 22:01:11 | 2014-03-20 16:41:03 | 2014-04-10     | 2014-06-30 |
| 252.01 | 2014-02-08 02:52:33 | 2014-03-20 16:43:03 | 2014-04-10     | 2014-06-30 |
| 123.02 | 2014-02-08 03:00:52 | 2014-03-17 18:58:10 | 2014-04-10     | 2014-06-30 |
| 213.01 | 2014-02-08 04:01:35 | 2014-03-26 19:03:01 | 2014-04-10     | 2014-09-19 |
|  55.01 | 2014-02-08 21:04:45 | 2014-03-07 18:40:46 | NULL           | 2014-06-26 |
| 126.01 | 2014-02-08 21:46:58 | 2014-09-02 18:39:36 | 2014-09-09     | 2014-09-26 |
| 284.01 | 2014-02-09 01:52:54 | 2014-06-11 19:11:06 | 2014-07-02     | 2014-07-21 |
| 261.01 | 2014-02-09 02:20:34 | 2014-03-17 20:57:39 | 2014-04-10     | 2014-06-30 |
| 318.01 | 2014-02-09 03:09:28 | 2014-03-17 20:44:25 | 2014-04-10     | 2014-06-30 |
| 225.01 | 2015-02-10 03:21:08 | 2014-03-20 16:57:56 | 2014-04-10     | 2014-06-30 |
| 248.01 | 2014-02-09 03:30:58 | 2014-03-18 18:02:21 | 2014-04-10     | 2014-06-30 |
| 178.01 | 2014-04-05 03:35:25 | 2014-03-21 17:10:12 | 2014-04-10     | 2014-06-30 |
| 184.01 | 2014-04-08 04:01:13 | 2015-03-20 16:38:02 | 2015-04-10     | 2015-06-30 |
| 320.01 | 2014-04-08 05:57:23 | 2015-03-17 20:49:19 | 2015-04-10     | 2015-06-30 |
| 230.02 | 2015-05-08 06:18:15 | 2016-03-20 16:41:08 | 2016-04-10     | 2016-06-06 |
| 325.01 | 2014-05-09 06:23:50 | 2015-03-17 20:42:04 | 2015-04-10     | 2015-06-30 |
+--------+---------------------+---------------------+----------------+------------+

需要获取下一个数据:

+---------+---------+--------+-----------+---------+
| year    | Created | Passed | To client | To prov |
+---------+---------+--------+-----------+---------+
| 2016-01 |    1901 |   1879 |      1873 |    1743 |
| 2016-02 |    2192 |   2169 |      2114 |    1912 |
| 2016-03 |    2693 |   2639 |      2539 |    2309 |
| 2016-04 |    2634 |   2574 |      2273 |    1976 |
| 2016-05 |    2593 |   2497 |      1109 |     949 |
| 2016-06 |     471 |    449 |         2 |      78 |
+---------+---------+--------+-----------+---------+

其中年份如DATE_FORMAT(curdate(), '%Y-%m'),下一栏Count(assistfin.crt_date)Created

问题是crt_date可能与2015%类似,但sent_to_clientdop_prov可能与2016%类似。

如何进行正确的查询?

2 个答案:

答案 0 :(得分:1)

好的抱歉,这是如此漫长而混乱,我也无法使用工会,因为我如此傲慢地在评论中发布,也必须参考MySQL: Is it possible to 'fill' a SELECT with values without a table?,这给了我几个月的清单。你可以重写它,所以你把所有表连接到crt_date,但是它不会显示一个月没有创建任何东西,因此生成的月表。原始查询在几个月内有120个限制,但我已将其替换为日期时间> '2014'让您在最早的日期之前改变。

试试这个,看看它运行的速度有多快。

select Months.yearmonth, created, passed, to_client, to_prov

from
(SELECT date_format(datetime,'%Y-%m') as yearmonth
FROM (
    select (curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) MONTH) as datetime
    from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
) AS t
where datetime > '2014' -- enter your earliest year here
ORDER BY datetime ASC) Months left join
(select date_format(crt_date,'%Y-%m') as yearmonth, count(no) as "created" from assistfin group by yearmonth) created on Months.yearmonth=created.yearmonth
left join
(select date_format(tobilling_date,'%Y-%m') as yearmonth, count(no) as "passed" from assistfin group by yearmonth) passed on Months.yearmonth=passed.yearmonth
left join
(select date_format(sent_to_client,'%Y-%m') as yearmonth, count(no) as "to_client" from assistfin group by yearmonth) to_client on Months.yearmonth=to_client.yearmonth
left join
(select date_format(dop_prov,'%Y-%m') as yearmonth, count(no) as "to_prov" from assistfin group by yearmonth) to_prov on Months.yearmonth=to_prov.yearmonth
where 
group by yearmonth;

答案 1 :(得分:0)

在其中使用group by和date_forma

select  date_format(crt_date, '%Y-%m') as year, count(sent_to_client ), count(dop_pprov) 
from assistfin 
where date_format(crt_date, '%Y-%m') = date_format(now(), '%Y-%m')
group by year

你可以

的那一年
select  date_format(crt_date, '%Y-%m') as year, count(sent_to_client ), count(dop_pprov) 
from assistfin 
where date_format(crt_date, '%Y') = date_format(now(), '%Y')
group by year

对于一系列年份的OR,你可以

select  date_format(crt_date, '%Y-%m') as year, count(sent_to_client ), count(dop_pprov) 
from assistfin 
where date_format(crt_date, '%Y') 
           BETWEEN(date_format(now(),'%Y')-2) and date_format(now(), '%Y') 
group by year