如何计算mysql中连续的重复行

时间:2017-09-27 10:30:12

标签: mysql sql

我们在表客户中有两列,其中我们有客户ID和服务ID。因此,每个使用多种服务的客户,我们都必须找到客户使用的连续服务的最大数量。

create table customer(id int not null auto_increment,customerid varchar(20), serviceid varchar(20),primary key(id));

insert into customer(customerid,serviceid) values('Nitesh','Mobile');
insert into customer(customerid,serviceid) values('Nitesh','Mobile');
insert into customer(customerid,serviceid) values('Nitesh','Landline');
insert into customer(customerid,serviceid) values('Nitesh','Broadband');
insert into customer(customerid,serviceid) values('Nitesh','Mobile');

insert into customer(customerid,serviceid) values('Nishant','Mobile');
insert into customer(customerid,serviceid) values('Nishant','Landline');
insert into customer(customerid,serviceid) values('Nishant','Landline');
insert into customer(customerid,serviceid) values('Nishant','Landline');
insert into customer(customerid,serviceid) values('Nishant','Broadband');

insert into customer(customerid,serviceid) values('Soe','Mobile');
insert into customer(customerid,serviceid) values('Soe','Mobile');
insert into customer(customerid,serviceid) values('Soe','Landline');
insert into customer(customerid,serviceid) values('Soe','Broadband');
insert into customer(customerid,serviceid) values('Soe','Mobile');

我必须连续计算客户使用的最大服务。

输出:

Customerid|Serviceid|ServiceidCount
----------------------------------

Nitesh|Mobile|2
Nishant|Landline|3
Soe|Mobile|2

2 个答案:

答案 0 :(得分:0)

我假设你有一个指定排序的列,比如id

然后你可以使用变量来做到这一点:

select customer, max(rn) as serviceidcnt,
       substring_index(group_concat(serviceid order by rn desc)) as serviceid
from (select c.*,
             (@rn := if(@cs = concat_ws(':', customerid, serviceid), @rn + 1,
                        if(@cs := concat_ws(':', customerid, serviceid), 1, 1)
                       )
             ) as rn
      from customer c cross join
           (select @rn := 0, @cs := '') params
      order by c.customer, ??
     ) cs
group by customer;

??用于订购的列。

注意:获取服务ID需要使用依赖于group_concat()的MySQL“hack”。默认情况下,这限制为大约1000字节的内部缓冲区大小。这可以改变。

答案 1 :(得分:0)

更新版本

您可以尝试以下查询。它使用mysql变量来检查列 customerid serviceid 中的连续更改。

    SELECT customerid, serviceid, MAX(R)+1 AS ServiceIdCount
FROM (SELECT c.customerid, c.serviceid
    , CASE WHEN @s=CONCAT(customerid,'|',serviceid) THEN @r:=@r+1 ELSE @r:=0 END AS R
    , @s:=CONCAT(customerid,'|',serviceid) AS S
    FROM customer c
    CROSS JOIN (SELECT @r:=0, @s:='') d
     ORDER BY id
    ) e 
WHERE R>0
GROUP BY customerid, serviceid;

添加了样本数据(不是你的):

insert into customer(customerid,serviceid) values('Mark','Mobile');
insert into customer(customerid,serviceid) values('Mark','Mobile');
insert into customer(customerid,serviceid) values('Mark','Mobile');
insert into customer(customerid,serviceid) values('Mark','Landline');
insert into customer(customerid,serviceid) values('Mark','Mobile');
insert into customer(customerid,serviceid) values('Mark','Mobile');

输出:

+------------+-----------+----------------+
| customerid | serviceid | ServiceIdCount |
+------------+-----------+----------------+
| Mark       | Mobile    |              3 |
| Nishant    | Landline  |              3 |
| Nitesh     | Mobile    |              2 |
| Soe        | Mobile    |              2 |
+------------+-----------+----------------+