我有一张包含以下数据的表
(1995, 'a'),
(1996, 'a'),
(1997, 'a'),
(1998, 'a'),
(2000, 'a'),
(1995, 'b'),
(1997, 'b'),
(1998, 'b'),
(1995, 'c'),
(1999, 'c'),
(2000, 'c'),
(1999, 'd'),
(1999, 'e'),
(1999, 'f');
我需要计算每个字母表的最大序列长度,例如:因为'a'在1995,1996,1997,1998中连续存在,'a'的最大序列长度为4.预期输出为:
a-4
b-2
c-2
d-1
e-1
f-1
不确定如何继续。
由于
答案 0 :(得分:2)
请考虑以下事项:
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table (year INT NOT NULL,string CHAR(1) NOT NULL,PRIMARY KEY(year,string));
INSERT INTO my_table VALUES
(1995, 'a'),
(1996, 'a'),
(1997, 'a'),
(1998, 'a'),
(2000, 'a'),
(1995, 'b'),
(1997, 'b'),
(1998, 'b'),
(1995, 'c'),
(1999, 'c'),
(2000, 'c'),
(1999, 'd'),
(1999, 'e'),
(1999, 'f');
SELECT string
, MAX(diff) FROM
( SELECT a.string
, a.year start
, MIN(c.year) end
, MIN(c.year) - a.year+1 diff
FROM my_table a
LEFT
JOIN my_table b
ON b.string = a.string
AND b.year + 1 = a.year
LEFT
JOIN my_table c
ON c.string = a.string
AND c.year >= a.year
LEFT
JOIN my_table d
ON d.string = a.string
AND d.year - 1 = c.year
WHERE b.string IS NULL
AND c.string IS NOT NULL
AND d.string IS NULL
GROUP
BY a.string,a.year
) a
GROUP
BY string;
+--------+-----------+
| string | MAX(diff) |
+--------+-----------+
| a | 4 |
| b | 2 |
| c | 2 |
| d | 1 |
| e | 1 |
| f | 1 |
+--------+-----------+
编辑:我对变量有一种审美厌恶,但事实上,在表现方面,他们的效率会更高......
SELECT string
, MAX(i)
FROM
(
SELECT year
, string
, IF(year=@prev_year+1,IF(string=@prev_string,@i:=@i+1,@i:=1),@i:=1)i
, @prev_year := year
, @prev_string := string
FROM my_table
, ( SELECT @prev_year=0,@prev_string:='',@i:=1 ) vars
ORDER
BY string
, year
) x
GROUP
BY string;
+--------+--------+
| string | MAX(i) |
+--------+--------+
| a | 4 |
| b | 2 |
| c | 2 |
| d | 1 |
| e | 1 |
| f | 1 |
+--------+--------+
答案 1 :(得分:0)
我认为下面的代码可以帮助你。
SELECT string, COUNT(string) AS MAX
FROM my_table
GROUP BY string
HAVING COUNT(string)>=1;