mysql计算同一季度的和

DROP TABLE IF EXISTS t_pro;
CREATE TABLE IF NOT EXISTS t_pro
(
months VARCHAR(10),
jidu VARCHAR(5),
sal_count INT
);

SELECT * FROM t_pro;
INSERT INTO t_pro VALUES(‘201501’,’1’, 1);
INSERT INTO t_pro VALUES(‘201502’,’1’, 2);
INSERT INTO t_pro VALUES(‘201503’,’1’, 1);
INSERT INTO t_pro VALUES(‘201504’,’2’, 1);
INSERT INTO t_pro VALUES(‘201505’,’2’, 3);
INSERT INTO t_pro VALUES(‘201506’,’2’, 1);
INSERT INTO t_pro VALUES(‘201507’,’3’, 1);
INSERT INTO t_pro VALUES(‘201508’,’3’, 5);
INSERT INTO t_pro VALUES(‘201509’,’3’, 1);
INSERT INTO t_pro VALUES(‘201510’,’4’,1);
INSERT INTO t_pro VALUES(‘201511’,’4’,7);
INSERT INTO t_pro VALUES(‘201512’,’4’, 1);

想得到:

set @jidu := ‘1’, @sal_count := 0;
select t.months,
t.jidu,
@sal_count := if(@jidu=t.jidu, @sal_count+t.sal_count, t.sal_count) sc,
@jidu := if(@jidu=t.jidu, @jidu, @jidu + 1) jd
from t_pro t

你可能感兴趣的:(mysql)