建表
create table if not exists test.tb_ab(A string,B int)row format delimited fields terminated by ",";
导入数据
vim /doit/tb_ab2010,12011,12012,12013,02014,02015,12016,12017,12018,02019,0
load data local inpath "/doit/tb_ab" into table test.tb_ab;
解法:
使用窗口函数
select a,b,c,row_number() over(partition by b,c order by a) as dfrom (select a,b,a - row_number() over(partition by b order by a ) as cfrom tb_ab)t
使用mysql变量
SELECt A ,B ,CFROM( SELECt A ,B ,@C := CASE WHEN @B = B THEN @C + 1 ELSE 1 END AS C ,@B := B AS B1 FROM tb_ab ) t