简体   繁体   English

MySQL-连续出现的次数

[英]MySQL - Count of consecutive occurrences

With respect to this fiddle , 关于这个小提琴

create table tbl(`date` date, customer varchar(200), 
                  serv_info varchar(200), category varchar(20));

insert into tbl values
('2015-01-01', 'customerA', 'Type1Id1', 'AG'),
('2015-01-02', 'customerA', 'Type1Id1', 'AG'),
('2015-01-03', 'customerA', 'Type1Id1', 'AG'),
('2015-01-11', 'customerA', 'Type1Id2', 'AG'),
('2015-01-13', 'customerA', 'Type1Id2', 'AG'),
('2015-01-16', 'customerA', 'Type1Id3', 'AG'),
('2015-01-20', 'customerA', 'Type2Id1', 'AG'),
('2015-01-21', 'customerA', 'Type2Id1', 'AG'),
('2015-01-22', 'customerA', 'Type2Id1', 'AG'),
('2015-01-23', 'customerA', 'Type2Id1', 'AG'),
('2015-01-11', 'customerA', 'Type1Id1', 'AG'),
('2015-01-12', 'customerA', 'Type1Id1', 'AG'),
('2015-01-13', 'customerA', 'Type1Id1', 'AG');

I'd like to see the below output - 我想看下面的输出-

10/01/15 - Type1Id1 -10
11/01/15 - Type1Id2 - 5
16/01/15 - Type1Id1 -3

I've managed to do the categorization with the SQL, however, I'm not able to fully achieve the requirement specially the consecutive count for the same server ID of a given type at various points in time. 我已经设法用SQL进行了分类,但是,我不能完全满足要求,特别是在不同时间点给定类型的同一服务器ID的连续计数。 (I'm not a full fledged MYSQL person & hence struggling to get this right) (我不是一个成熟的MYSQL人,因此很难做到这一点)

Could I please request help to get this working fully. 我能否要求帮助以使它完全正常工作。 I've tried my best toe explain the scenario. 我已经尽力解释了这种情况。 if it's unclear, please do let me know what is unclear & I'll try to restate it. 如果不清楚,请让我知道不清楚的地方,我将尝试重述。

Here is a solution based on aggregate function, group by and User variable. 这是一个基于聚合函数,分组依据和用户变量的解决方案。 User variable is used here to guarantee the consequence. 用户变量在此处用于保证结果。

The output is also slightly formatted to match the requirement. 输出也会略微格式化以符合要求。

SQL: SQL:

-- Data preparation
create table tbl(`date` date, customer varchar(200), serv_info varchar(200), category varchar(20));
insert into tbl values
('2015-01-01', 'customerA', 'Type1Id1', 'AG'),
('2015-01-02', 'customerA', 'Type1Id1', 'AG'),
('2015-01-03', 'customerA', 'Type1Id1', 'AG'),
('2015-01-11', 'customerA', 'Type1Id2', 'AG'),
('2015-01-13', 'customerA', 'Type1Id2', 'AG'),
('2015-01-16', 'customerA', 'Type1Id3', 'AG'),
('2015-01-20', 'customerA', 'Type2Id1', 'AG'),
('2015-01-21', 'customerA', 'Type2Id1', 'AG'),
('2015-01-22', 'customerA', 'Type2Id1', 'AG'),
('2015-01-23', 'customerA', 'Type2Id1', 'AG'),
('2015-01-11', 'customerA', 'Type1Id1', 'AG'), 
('2015-01-12', 'customerA', 'Type1Id1', 'AG'), 
('2015-01-13', 'customerA', 'Type1Id1', 'AG');
SELECT * FROM tbl;

-- Needed
SET @rownum = 0;
SET @typeid = '';
SELECT 
    CONCAT( DATE_FORMAT(assign_date, '%d/%m/%y'), ' - ', tbl2.serv_info, ' - ', tbl2.consecutive_days ) Output
FROM 
    (SELECT 
        MIN(`date`) assign_date, 
        serv_info, 
        COUNT(1) consecutive_days,
        @rownum:=@rownum+(serv_info != @typeid) conse_group,
        @typeid:=serv_info
    FROM tbl
    WHERE customer = 'customerA'
    GROUP BY conse_group) tbl2;

Output: 输出:

mysql> SELECT * FROM tbl;
+------------+-----------+-----------+----------+
| date       | customer  | serv_info | category |
+------------+-----------+-----------+----------+
| 2015-01-01 | customerA | Type1Id1  | AG       |
| 2015-01-02 | customerA | Type1Id1  | AG       |
| 2015-01-03 | customerA | Type1Id1  | AG       |
| 2015-01-11 | customerA | Type1Id2  | AG       |
| 2015-01-13 | customerA | Type1Id2  | AG       |
| 2015-01-16 | customerA | Type1Id3  | AG       |
| 2015-01-20 | customerA | Type2Id1  | AG       |
| 2015-01-21 | customerA | Type2Id1  | AG       |
| 2015-01-22 | customerA | Type2Id1  | AG       |
| 2015-01-23 | customerA | Type2Id1  | AG       |
| 2015-01-11 | customerA | Type1Id1  | AG       |
| 2015-01-12 | customerA | Type1Id1  | AG       |
| 2015-01-13 | customerA | Type1Id1  | AG       |
+------------+-----------+-----------+----------+
13 rows in set (0.00 sec)

mysql>
mysql> -- Needed
mysql> SET @rownum = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> SET @typeid = '';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT
    -> CONCAT( DATE_FORMAT(assign_date, '%d/%m/%y'), ' - ', tbl2.serv_info, ' - ', tbl2.consecutive_days ) Output
    -> FROM
    ->     (SELECT
    ->     MIN(`date`) assign_date,
    ->     serv_info,
    ->     COUNT(1) consecutive_days,
    ->     @rownum:=@rownum+(serv_info != @typeid) conse_group,
    ->     @typeid:=serv_info
bl2;
    ->     FROM tbl
    ->     WHERE customer = 'customerA'
    ->     GROUP BY conse_group) tbl2;
+-------------------------+
| Output                  |
+-------------------------+
| 01/01/15 - Type1Id1 - 3 |
| 11/01/15 - Type1Id2 - 2 |
| 16/01/15 - Type1Id3 - 1 |
| 20/01/15 - Type2Id1 - 4 |
| 11/01/15 - Type1Id1 - 3 |
+-------------------------+
5 rows in set (0.00 sec)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM