有谁知道如何从表格中的每一列获取最大序列计数。例如,如果表A具有以下数据
id | n1 | n2 | n3 | n4 | n5 | n6 | n7 | n8 | n9 | n10| Max | Sets
----------------------------------------------------------------
1 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 10 | 1 |
2 | 1 | 3 | 4 | 5 | 6 | 8 | 14 | 15 | 16 | 25 | 4 | 2 |
3 | 4 | 8 | 9 | 15 | 18 | 19 | 55 | 64 | 65 | 98 | 2 | 3 |
4 | 41 | 55 | 66 | 67 | 68 | 69 | 74 | 82 | 131| 132| 4 | 2 |
从上表中可以看出,我需要从一行中获得最大的连续数字,在上面的示例中,我添加了“Max”和“amp”列。 '设置'以显示所需的结果。
在第一行中有10个连续数字(1到10)和只有1组/连续数字,在第二行中只有4(3,4,5,6)和2组连续数字( 3至6和14至16)。
我一直试图解决这个问题很长一段时间,但无法理解所需的查询(即加入或直接选择和if语句)。我可以用其他语言做到这一点,但更愿意让MySQL完成这个计算。
FOR i <= count(column)
IF count > sequence
THEN sequence = count
ELSE count ++
NEXT i
SQL Fiddle包含我的表和数据,我正在尝试进行必要的查询
答案 0 :(得分:1)
抱歉,我一开始并不了解有关SQLFiddle的评论。我在那里测试了代码,发现我需要修复更多内容。
查询需要将n *列从最终的group-by表达式移动到聚合,以及a(在第一个FROM和第二个SELECT之间。 - 和一个COUNT()&gt; = 2而不是COUNT()&gt; 2.
现在,当您运行测试代码时,它可以正常工作 以下是测试http://sqlfiddle.com/#!2/9e3cb/21/0
的网址更正的答案:
SELECT id, MAX(n1) n1, MAX(n2) n2, MAX(n3) n3, MAX(n4) n4, MAX(n5) n5,
MAX(n6) n6, MAX(n7) n7, MAX(n8) n8, MAX(n9) n9, MAX(n10) n10,
MAX(GrpSize) Max, COUNT(GrpNumber) Sets
FROM (
SELECT id, n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, GrpNumber, COUNT(*) GrpSize
FROM (
SELECT id, n1, n2, n3, n4, n5, n6, n7, n8, n9, n10,
CASE Numbers.N
WHEN 1 THEN n1 - N
WHEN 2 THEN n2 - N
WHEN 3 THEN n3 - N
WHEN 4 THEN n4 - N
WHEN 5 THEN n5 - N
WHEN 6 THEN n6 - N
WHEN 7 THEN n7 - N
WHEN 8 THEN n8 - N
WHEN 9 THEN n9 - N
WHEN 10 THEN n10 - N
END GrpNumber
FROM `mytbl`
CROSS JOIN (
SELECT 1 AS N UNION ALL
SELECT 2 AS N UNION ALL
SELECT 3 AS N UNION ALL
SELECT 4 AS N UNION ALL
SELECT 5 AS N UNION ALL
SELECT 6 AS N UNION ALL
SELECT 7 AS N UNION ALL
SELECT 8 AS N UNION ALL
SELECT 9 AS N UNION ALL
SELECT 10 AS N
) Numbers
) TT
GROUP BY id, n1, n2, n3, n4, n5, n6, n7, n8, n9, n10,GrpNumber
HAVING COUNT(*) >= 2
) TT
GROUP BY id
原答案:
这是使用基于集合的查询执行此操作的方法。此查询假设您的表名为TableOfTen,并且它具有正好10 n [umber] [1..10]列。它可以针对任何预先知道n [number] [...]列数的表(或派生表)进行修改。
SELECT id, n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, MAX(GrpSize) MaxSize, COUNT(GrpNumber) NumberOfSetsWithTwoOrMoreMembers
FROM (
SELECT id, n1, n2, n3, n4, n5, n6, n7, n8, n9, n10,GrpNumber, COUNT(*) GrpSize
FROM
SELECT id, n1, n2, n3, n4, n5, n6, n7, n8, n9, n10,
CASE Numbers.N
WHEN 1 THEN n1 - N
WHEN 2 THEN n2 - N
WHEN 3 THEN n3 - N
WHEN 4 THEN n4 - N
WHEN 5 THEN n5 - N
WHEN 6 THEN n6 - N
WHEN 7 THEN n7 - N
WHEN 8 THEN n8 - N
WHEN 9 THEN n9 - N
WHEN 10 THEN n10 - N
END GrpNumber
FROM `mytbl`
CROSS JOIN (
SELECT 1 AS N UNION ALL
SELECT 2 AS N UNION ALL
SELECT 3 AS N UNION ALL
SELECT 4 AS N UNION ALL
SELECT 5 AS N UNION ALL
SELECT 6 AS N UNION ALL
SELECT 7 AS N UNION ALL
SELECT 8 AS N UNION ALL
SELECT 9 AS N UNION ALL
SELECT 10 AS N
) Numbers
) TT
GROUP BY id, n1, n2, n3, n4, n5, n6, n7, n8, n9, n10,GrpNumber
HAVING COUNT(*) > 2
) TT
GROUP BY id, n1, n2, n3, n4, n5, n6, n7, n8, n9, n10
答案 1 :(得分:0)
declare c1,cnt,max,cmax,pv,cv,sets int;
在解释变量后,我觉得只需要告诉以下重复条件。
if cv=pv+1 then set cmax=cmax+1;
if cmax=max then set sets=sets+1; end if;
if cmax>max then set max=cmax; end if;
else set cmax=1;end if;set pv=cv;
cv=pv+1
=&gt;当前列值=上一列然后我们将增加cmax cmax=cmax+1;
,我们还需要两次检查
if cmax=max then sets=sets+1;
和
if cmax>max then max=cmax;
以及cv!=pv+1
然后cmax=1
我使用了临时表,为每行插入了Max_Count和No_of_Sets值。最后显示
希望它有助于你想要的东西:)