Mysql SQL从同一列中查找增量值的最大值

Mysql SQL从同一列中查找增量值的最大值,mysql,sql,database,sqlite,Mysql,Sql,Database,Sqlite,查找: 最大abs ca=2-ca=1 b订的 给定表T a b c -- -- -- 1 1 2 1 2 4 1 3 5 2 1 10 2 2 11 2 3 20 3 1 40 3 2 40 3 3 40 数学示例: c(@2) - c(@1) in order of b 10 - 2 = 8 11 - 4 = 7 20 - 5 = 15 <-- max found 我还不明白你的意思。

查找: 最大abs ca=2-ca=1 b订的

给定表T

a  b  c
-- -- --
1  1  2      
1  2  4
1  3  5
2  1  10
2  2  11
2  3  20
3  1  40
3  2  40
3  3  40
数学示例:

c(@2) - c(@1) in order of b
10    - 2     = 8
11    - 4     = 7
20    - 5     = 15  <-- max found

我还不明白你的意思。 这可能会有所帮助

DROP TABLE IF EXISTS t;
CREATE TABLE t( id integer,f integer, x integer);

INSERT INTO t VALUES (1,1,2), (1,2,4), (1,3,5);
INSERT INTO t VALUES (2,1,10), (2,2,11), (2,3,20);
INSERT INTO t VALUES (3,1,40), (3,2,40), (3,3,40);

SELECT * from t;

SELECT MAX(ABS(sup.x - inf.x))
FROM  (SELECT f, MAX(x) x from t WHERE id = 2 GROUP BY f) sup
      INNER JOIN (select f, MIN(x) x FROM t WHERE id = 1 GROUP BY f) inf ON sup.f = inf.f

使用

测试这里有一个尝试使用lead函数对数据进行排序

create table t(a int, b int , c int)

insert into t values(1,1,2)      
insert into t values(1,2,4)
insert into t values(1,3,5)
insert into t values(2,1,10)
insert into t values(2,2,11)
insert into t values(2,3,20)
insert into t values(3,1,40)
insert into t values(3,2,40)
insert into t values(3,3,40)

with data
  as (select * /*First rank the results on the basis of columns a and b*/    
            ,row_number() over(order by a,b) as rnk
        from t
     )
,temp_data
  as (select a      
            ,b
            ,c
            ,rnk
            ,abs(c-lead(c,3) over(order by rnk)) as max_rnk
       from data
      )
select a,max(max_rnk) /*Gets the max value of rank grouped by each a*/      
  from temp_data
group by a
使用dbfiddle演示


你的问题是否等同于max of x,其中id=2-min of x,其中id=1?否,查询必须计算与f同步的增量。我看不到预期结果和你尝试的查询。我显示了预期结果=15?以下两个答案都有效:我修改了表格以匹配Mani Kandan的答案,这似乎有效!让我再检查一下…干得好,伙计!我欠你几杯啤酒!选择maxabsa.a_c-b.b_c作为cdiff,从中选择c作为a_c,从t中选择b作为ss,其中a=1作为连接,从t中选择c作为b_c,从t中选择b作为ssr,其中a=3作为a.b=b.b@skywalk在SO上说谢谢的常用方式是接受答案。对不起,时间太晚了。我将在上面编辑以显示缩小的工作查询。谢谢,谢谢你,你的回答也是正确的。我被你的选择中额外的stuffMAX、MIN和GROUP by弄糊涂了。可能是在调试查询时发生的?但是,它们不是最终结果所必需的。
DROP TABLE IF EXISTS t;
CREATE TABLE t( id integer,f integer, x integer);

INSERT INTO t VALUES (1,1,2), (1,2,4), (1,3,5);
INSERT INTO t VALUES (2,1,10), (2,2,11), (2,3,20);
INSERT INTO t VALUES (3,1,40), (3,2,40), (3,3,40);

SELECT * from t;

SELECT MAX(ABS(sup.x - inf.x))
FROM  (SELECT f, MAX(x) x from t WHERE id = 2 GROUP BY f) sup
      INNER JOIN (select f, MIN(x) x FROM t WHERE id = 1 GROUP BY f) inf ON sup.f = inf.f
create table t(a int, b int , c int)

insert into t values(1,1,2)      
insert into t values(1,2,4)
insert into t values(1,3,5)
insert into t values(2,1,10)
insert into t values(2,2,11)
insert into t values(2,3,20)
insert into t values(3,1,40)
insert into t values(3,2,40)
insert into t values(3,3,40)

with data
  as (select * /*First rank the results on the basis of columns a and b*/    
            ,row_number() over(order by a,b) as rnk
        from t
     )
,temp_data
  as (select a      
            ,b
            ,c
            ,rnk
            ,abs(c-lead(c,3) over(order by rnk)) as max_rnk
       from data
      )
select a,max(max_rnk) /*Gets the max value of rank grouped by each a*/      
  from temp_data
group by a