Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 参考上面的单元格_Mysql_Cell - Fatal编程技术网

Mysql 参考上面的单元格

Mysql 参考上面的单元格,mysql,cell,Mysql,Cell,我想了解有关上面单元格的mysql查询的帮助。 例如,在下表中: primary key(id) day count percentage change 1 monday 1 0 2 tuesday 2 (1-0)*100%=100% 3

我想了解有关上面单元格的mysql查询的帮助。 例如,在下表中:

primary key(id)     day             count           percentage change
1                  monday             1               0  
2                  tuesday            2              (1-0)*100%=100%  
3                  wednesday          5              (2-1)*100%=100%  
4                  thursday           9              (5-2)*100%=300%  
5                  friday             27             (9-5)*100%=400%  

百分比变化结果基于计数列前两天的结果。有没有办法合并主键ID以引用上面的单元格?

没有一种简单的方法。您需要使用自联接

select t.*,
       (coalesce(t_1.count, 0) - coalesce(t_2.count, 0)) * 100.0
from t left outer join
     t t_1
     on t.id = t_1.id + 1 left outer join
     t t_2
     on t.id = t_2.id + 2
左外部联接确保所有原始行保持不变,即使前面没有ID

这是因为ID是连续的。如果ID不是连续的,并且计数是单调递增的,则可以使用相关子查询执行此操作:

select t.*,
       (coalesce((select max(`count`) as val
                  from table1 t_1
                  where t_1.`count` < t.`count`
                 ), 0)
       ) -
       (coalesce((select max(`count`)
                  from table1 t_2
                  where t_2.`count` < (select max(`count`) from table1 t_1 where t_1.`count` < t.`count`)
                 ), 0)
       )
from table1 t
注意:如果一行中的两个值相同,则这将无法正常工作。为此,您需要改用id:

select t.*,
       (coalesce((select max(`count`) as val
                  from table1 t_1
                  where t_1.`id` < t.`id`
                 ), 0)
       ) -
       (coalesce((select max(`count`)
                  from table1 t_2
                  where t_2.`id` < (select max(`id`) from table1 t_1 where t_1.`id` < t.`id`)
                 ), 0)
       )
from table1 t
如果计数没有增加,则必须获取ID,并再次加入值。多有趣啊!代码如下:

select t.*,
       coalesce(t1.count, 0) - coalesce(t2.count, 0)
from (select t.*,
             (select max(`id`) as id1 from table1 t_1 where t_1.`id` < t.`id`
             ) as id1,
             (select max(`count`) from table1 t_2
              where t_2.`id` < (select max(`id`) from table1 t_1 where t_1.`id` < t.`id`)
             ) id2
      from table1 t
     ) t left outer join
     table1 t1
     on t.id1 = t1.id left outer join
     table1 t2
     on t.id2 = t2.id

星期四的百分比变化是星期二和星期三之间的变化吗?我在小提琴上试过这个。星期一和星期二是对的,但星期三、星期四和星期五是否定的。你知道为什么吗?另外,coalesce只是意味着如果t_1.count为null,那么将0改为null,对吗?您能看看我的新查询吗?我将select分组并称之为t,而不是表t。有一个错误表示表t不存在,但它不存在。它只是一个团体。有没有办法用一个组而不是一个表来实现你的方法,t?谢谢我有两个问题。第一个是关于新代码本身。我试着把它放到小提琴上,不幸的是有一个语法错误,我无法发现。。。。。另外,当计数单调递增时,您能解释一下相关子查询的代码吗?在小提琴中,计数并不是真的线性增加。作为所有不同数据集(每个数据集的计数为1)的汇编,最终的计数结果没有明显的模式。关于我前面的问题,如果我继续我的代码,通过分组求count1s的和,我能用一个组而不是一个表t实现你的方法吗?@user1569897。我用最后一列修正了这个问题。您需要一个id或时间戳或其他东西来指定行的正确顺序。