1. <small id='Kewrx'></small><noframes id='Kewrx'>

    • <bdo id='Kewrx'></bdo><ul id='Kewrx'></ul>
      <legend id='Kewrx'><style id='Kewrx'><dir id='Kewrx'><q id='Kewrx'></q></dir></style></legend>

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

      <tfoot id='Kewrx'></tfoot>
    2. SQL:选择列值从上一行更改的行

      SQL: selecting rows where column value changed from previous row(SQL:选择列值从上一行更改的行)
      <legend id='al1Mw'><style id='al1Mw'><dir id='al1Mw'><q id='al1Mw'></q></dir></style></legend>

    3. <tfoot id='al1Mw'></tfoot>
        <tbody id='al1Mw'></tbody>

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

          <i id='al1Mw'><tr id='al1Mw'><dt id='al1Mw'><q id='al1Mw'><span id='al1Mw'><b id='al1Mw'><form id='al1Mw'><ins id='al1Mw'></ins><ul id='al1Mw'></ul><sub id='al1Mw'></sub></form><legend id='al1Mw'></legend><bdo id='al1Mw'><pre id='al1Mw'><center id='al1Mw'></center></pre></bdo></b><th id='al1Mw'></th></span></q></dt></tr></i><div id='al1Mw'><tfoot id='al1Mw'></tfoot><dl id='al1Mw'><fieldset id='al1Mw'></fieldset></dl></div>
              • <bdo id='al1Mw'></bdo><ul id='al1Mw'></ul>
              • 本文介绍了SQL:选择列值从上一行更改的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                假设我有这个(MySQL)数据库,按增加时间戳排序:

                Let's say I have this (MySQL) database, sorted by increasing timestamp:

                Timestamp   System StatusA StatusB 
                2011-01-01     A      Ok     Ok      
                2011-01-02     B      Ok     Ok     
                2011-01-03     A     Fail   Fail     
                2011-01-04     B      Ok    Fail     
                2011-01-05     A     Fail    Ok      
                2011-01-06     A      Ok     Ok      
                2011-01-07     B     Fail   Fail    
                

                如何为该系统选择 StatusA 从前一行更改的行?StatusB 无关紧要(我在这个问题中显示它只是为了说明每个系统可能有许多连续的行,其中 StatusA 没有改变).在上面的示例中,查询应返回行 2011-01-03(对于 SystemA,StatusA 在 2011-01-01 和 2011-01-03 之间更改)、2011-01-06、2011-01-07.

                How do I select the rows where StatusA changed from the previous row for that system? StatusB doesn't matter (I show it in this question only to illustrate that there may be many consecutive rows for each system where StatusA doesn't change). In the example above, the query should return the rows 2011-01-03 (StatusA changed between 2011-01-01 and 2011-01-03 for SystemA), 2011-01-06, 2011-01-07.

                对于包含数万条记录的表,查询应该快速执行.

                The query should execute quickly with the table having tens of thousands of records.

                谢谢

                推荐答案

                SELECT a.*
                FROM tableX AS a
                WHERE a.StatusA <>
                      ( SELECT b.StatusA
                        FROM tableX AS b
                        WHERE a.System = b.System
                          AND a.Timestamp > b.Timestamp
                        ORDER BY b.Timestamp DESC
                        LIMIT 1
                      ) 
                

                但是你也可以试试这个(在(System,Timestamp)上有一个索引:

                But you can try this as well (with an index on (System,Timestamp):

                SELECT System, Timestamp, StatusA, StatusB
                FROM
                  ( SELECT (@statusPre <> statusA AND @systemPre=System) AS statusChanged
                         , System, Timestamp, StatusA, StatusB
                         , @statusPre := StatusA
                         , @systemPre := System
                    FROM tableX
                       , (SELECT @statusPre:=NULL, @systemPre:=NULL) AS d
                    ORDER BY System
                           , Timestamp
                  ) AS good
                WHERE statusChanged ;
                

                这篇关于SQL:选择列值从上一行更改的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
                SQL query to group by day(按天分组的 SQL 查询)
                What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                Include missing months in Group By query(在 Group By 查询中包含缺失的月份)

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

                  • <legend id='9nc8w'><style id='9nc8w'><dir id='9nc8w'><q id='9nc8w'></q></dir></style></legend>
                        <tbody id='9nc8w'></tbody>

                    1. <tfoot id='9nc8w'></tfoot>
                        <bdo id='9nc8w'></bdo><ul id='9nc8w'></ul>