• <bdo id='8mzZh'></bdo><ul id='8mzZh'></ul>
      <tfoot id='8mzZh'></tfoot>

        <small id='8mzZh'></small><noframes id='8mzZh'>

        <legend id='8mzZh'><style id='8mzZh'><dir id='8mzZh'><q id='8mzZh'></q></dir></style></legend>
        <i id='8mzZh'><tr id='8mzZh'><dt id='8mzZh'><q id='8mzZh'><span id='8mzZh'><b id='8mzZh'><form id='8mzZh'><ins id='8mzZh'></ins><ul id='8mzZh'></ul><sub id='8mzZh'></sub></form><legend id='8mzZh'></legend><bdo id='8mzZh'><pre id='8mzZh'><center id='8mzZh'></center></pre></bdo></b><th id='8mzZh'></th></span></q></dt></tr></i><div id='8mzZh'><tfoot id='8mzZh'></tfoot><dl id='8mzZh'><fieldset id='8mzZh'></fieldset></dl></div>

        模拟 MySQL 中的滞后函数

        时间:2023-08-18
          <bdo id='D6VL7'></bdo><ul id='D6VL7'></ul>

          <i id='D6VL7'><tr id='D6VL7'><dt id='D6VL7'><q id='D6VL7'><span id='D6VL7'><b id='D6VL7'><form id='D6VL7'><ins id='D6VL7'></ins><ul id='D6VL7'></ul><sub id='D6VL7'></sub></form><legend id='D6VL7'></legend><bdo id='D6VL7'><pre id='D6VL7'><center id='D6VL7'></center></pre></bdo></b><th id='D6VL7'></th></span></q></dt></tr></i><div id='D6VL7'><tfoot id='D6VL7'></tfoot><dl id='D6VL7'><fieldset id='D6VL7'></fieldset></dl></div>

            <legend id='D6VL7'><style id='D6VL7'><dir id='D6VL7'><q id='D6VL7'></q></dir></style></legend>
              <tbody id='D6VL7'></tbody>

            <small id='D6VL7'></small><noframes id='D6VL7'>

            • <tfoot id='D6VL7'></tfoot>

                1. 本文介绍了模拟 MySQL 中的滞后函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  | time                | company | quote |
                  +---------------------+---------+-------+
                  | 0000-00-00 00:00:00 | GOOGLE  |    40 |
                  | 2012-07-02 21:28:05 | GOOGLE  |    60 |
                  | 2012-07-02 21:28:51 | SAP     |    60 |
                  | 2012-07-02 21:29:05 | SAP     |    20 |
                  

                  我如何在 MySQL 中对这个表做滞后以打印引号中的差异,例如:

                  How do I do a lag on this table in MySQL to print the difference in quotes, for example:

                  GOOGLE | 20
                  SAP    | 40  
                  

                  推荐答案

                  这是我最喜欢的 MySQL hack.

                  This is my favorite MySQL hack.

                  这是模拟滞后函数的方式:

                  This is how you emulate the lag function:

                  SET @quot=-1;
                  select time,company,@quot lag_quote, @quot:=quote curr_quote
                    from stocks order by company,time;
                  

                  • lag_quote 保存前一行引用的值.对于第一行,@quot 是 -1.
                  • curr_quote 保存当前行的引用值.
                    • lag_quote holds the value of previous row's quote. For the first row @quot is -1.
                    • curr_quote holds the value of current row's quote.
                    • 注意事项:

                      1. order by 子句在这里很重要,就像在常规中一样窗函数.
                      2. 您可能还想对 company 使用滞后,以确保您计算的是同一 company 的引号中的差异.
                      3. 你也可以用同样的方式实现行计数器@cnt:=@cnt+1
                      1. order by clause is important here just like it is in a regular window function.
                      2. You might also want to use lag for company just to be sure that you are computing difference in quotes of the same company.
                      3. You can also implement row counters in the same way @cnt:=@cnt+1

                      与使用聚合函数、存储过程或在应用服务器中处理数据等其他一些方法相比,这种方案的优点是计算量非常小.

                      The nice thing about this scheme is that is computationally very lean compared to some other approaches like using aggregate functions, stored procedures or processing data in application server.

                      现在来解决以您提到的格式获取结果的问题:

                      Now coming to your question of getting result in the format you mentioned:

                      SET @quot=0,@latest=0,company='';
                      select B.* from (
                      select A.time,A.change,IF(@comp<>A.company,1,0) as LATEST,@comp:=A.company as company from (
                      select time,company,quote-@quot as change, @quot:=quote curr_quote
                      from stocks order by company,time) A
                      order by company,time desc) B where B.LATEST=1;
                      

                      嵌套是不相关的,所以没有它看起来(语法上)那么糟糕(计算上):)

                      The nesting is not co-related so not as bad (computationally) as it looks (syntactically) :)

                      如果您需要任何帮助,请告诉我.

                      Let me know if you need any help with this.

                      这篇关于模拟 MySQL 中的滞后函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 MySQL 中生成一系列数字 下一篇:相当于explode() 在MySQL 中处理字符串

                  相关文章

                  最新文章

                      <i id='PDt9g'><tr id='PDt9g'><dt id='PDt9g'><q id='PDt9g'><span id='PDt9g'><b id='PDt9g'><form id='PDt9g'><ins id='PDt9g'></ins><ul id='PDt9g'></ul><sub id='PDt9g'></sub></form><legend id='PDt9g'></legend><bdo id='PDt9g'><pre id='PDt9g'><center id='PDt9g'></center></pre></bdo></b><th id='PDt9g'></th></span></q></dt></tr></i><div id='PDt9g'><tfoot id='PDt9g'></tfoot><dl id='PDt9g'><fieldset id='PDt9g'></fieldset></dl></div>
                      <legend id='PDt9g'><style id='PDt9g'><dir id='PDt9g'><q id='PDt9g'></q></dir></style></legend>
                      • <bdo id='PDt9g'></bdo><ul id='PDt9g'></ul>
                    1. <tfoot id='PDt9g'></tfoot>

                      <small id='PDt9g'></small><noframes id='PDt9g'>